Tag: 互斥

这是正确使用互斥锁吗?

我有一种情况,我可能会同时运行一个程序的多个实例,并且重要的是只有一个特定的函数不能同时在多个这些实例中执行。 这是使用互斥锁来防止这种情况发生的正确方法吗? lock (this.GetType()) { _log.Info(“Doing Sync”); DoSync(); _log.Info(“Sync Completed”); }

如何在C#中找到Mutex?

如何从C#中的互斥锁中找到获取互斥锁的? 当mutex.WaitOne(timeout)超时时,它返回false 。 但是,如何从互斥锁手柄中找到它? (也许使用p / invoke。) 更新 : public class InterProcessLock : IDisposable { readonly Mutex mutex; public bool IsAcquired { get; private set; } public InterProcessLock(string name, TimeSpan timeout) { bool created; var security = new MutexSecurity(); security.AddAccessRule(new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow)); mutex = new Mutex(false, name, out created, security); […]

什么是互斥锁和信号量在c#中? 我们需要实施的地方?

C#中的互斥锁和信号量是什么? 我们需要实施的地方? 我们如何在multithreading中与他们合作?

为什么来自线程教程的MSDN示例崩溃?

来自MSDN “线程教程”的示例4 以下代码错误出现在“—错误就在这里—”的行中。 怎么了? using System; using System.Threading; public class MutexSample { static Mutex gM1; static Mutex gM2; const int ITERS = 100; static AutoResetEvent Event1 = new AutoResetEvent(false); static AutoResetEvent Event2 = new AutoResetEvent(false); static AutoResetEvent Event3 = new AutoResetEvent(false); static AutoResetEvent Event4 = new AutoResetEvent(false); public static void Main(String[] args) { Console.WriteLine(“Mutex Sample […]

跨用户C#互斥体

我的应用程序被迫使用第三方模块,如果在同一台机器上同时启动两个实例,它将蓝屏Windows。 要解决此问题,我的C#应用​​程序有一个互斥锁: static Mutex mutex = new Mutex(true, “{MyApp_b9d19f99-b83e-4755-9b11-d204dbd6d096}”); 我检查它是否存在 – 如果是,我显示错误消息并关闭应用程序: bool IsAnotherInstanceRunning() { if (mutex.WaitOne(TimeSpan.Zero, true)) return (true); else return (false); } 问题是如果两个用户可以同时登录并打开应用程序,IsAnotherInstanceRunning()将返回false。 我该如何解决这个问题?

从不同步的代码块调用对象同步方法。 Mutex.Release()的例外情况

我找到了关于这个例外的不同文章,但没有一个是我的情况。 这是源代码: class Program { private static Mutex mutex; private static bool mutexIsLocked = false; static void Main(string[] args) { ICrmService crmService = new ArmenianSoftware.Crm.Common.CrmServiceWrapper(GetCrmService(“Armsoft”, “crmserver”)); //Lock mutex for concurrent access to workflow mutex = new Mutex(true, “ArmenianSoftware.Crm.Common.FilterCtiCallLogActivity”); mutexIsLocked = true; //Create object for updating filtered cti call log ArmenianSoftware.Crm.Common.FilterCtiCallLog filterCtiCallLog = new ArmenianSoftware.Crm.Common.FilterCtiCallLog(crmService); //Bind […]

单个应用程序实例的WPF互斥锁无法正常工作

我正在尝试使用互斥方法只允许我的应用程序的一个实例运行。 也就是说 – 我只想为一台机器上的所有用户提供最多一个实例。 我已经阅读了关于这个问题的各种其他线程,解决方案看起来很简单但是在测试中我不能让我的第二个实例不能运行。 这是我的代码…… public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { // check that there is only one instance of the control panel running… bool createdNew = true; using (Mutex instanceMutex = new Mutex(true, @”Global\ControlPanel”, out createdNew)) { if (!createdNew) { Application.Current.Shutdown(); return; } } base.OnStartup(e); } […]

如何在C#中创建系统互斥锁?

如何创建系统/多进程Mutex以使用相同的非托管资源协调多个进程。 背景:我编写了一个使用文件打印机的程序,它一次只能由一个进程使用。 如果我想在计算机上运行的多个程序上使用它,我需要一种在整个系统中同步它的方法。

如何让“Everyone”的IdentityReference在本地化系统上创建MutexAccessRule?

我想使用此问题或此问题中的代码来创建系统范围的互斥锁。 这在大多数系统上都可以正常工作,但在德语XP安装中,我收到“无法翻译某些或所有身份引用”的错误。 问题是“Everyone”组在这个系统上被称为“Alle”。 那么如何在所有独立于OS语言的系统上获得正确的IdentityReference来为“Everone”或“Users”创建MutexAccessRule?

在c#中使用互斥锁

我在c#的线程中有点新,在一般情况下,在我的程序中,我使用mutex只允许1个线程进入一个关键部分,并且由于未知原因做了一些cw打印,我可以看到超过1个线程进入我的关键部分,这是我的代码: Mutex m = new Mutex(); m.WaitOne(); // critical section here m.ReleaseMutex(); 我非常想知道我是否在这里犯了一个错误,提前感谢您的帮助。 编辑: 我的代码包括类,所以它基本上看起来像这样: public class test { private mutex m; public test() { m = new mutex(); } public func() { m.WaitOne(); // critical section here m.ReleaseMutex(); } }