使用Mutex运行应用程序的单个实例

为了只允许运行一个应用程序的单个实例,我正在使用互斥锁。 代码如下。 这是正确的方法吗? 代码中是否有任何缺陷?

当用户第二次尝试打开应用程序时,如何显示已在运行的应用程序。 目前(在下面的代码中),我只是显示另一个实例已在运行的消息。

static void Main(string[] args) { Mutex _mut = null; try { _mut = Mutex.OpenExisting(AppDomain.CurrentDomain.FriendlyName); } catch { //handler to be written } if (_mut == null) { _mut = new Mutex(false, AppDomain.CurrentDomain.FriendlyName); } else { _mut.Close(); MessageBox.Show("Instance already running"); } } 

我这样做了一次,我希望它有所帮助:

 bool createdNew; Mutex m = new Mutex(true, "myApp", out createdNew); if (!createdNew) { // myApp is already running... MessageBox.Show("myApp is already running!", "Multiple Instances"); return; } 
 static void Main() { using(Mutex mutex = new Mutex(false, @"Global\" + appGuid)) { if(!mutex.WaitOne(0, false)) { MessageBox.Show("Instance already running"); return; } GC.Collect(); Application.Run(new Form1()); } } 

资料来源: http : //odetocode.com/Blogs/scott/archive/2004/08/20/401.aspx

我用这个:

  private static Mutex _mutex; private static bool IsSingleInstance() { _mutex = new Mutex(false, _mutexName); // keep the mutex reference alive until the normal //termination of the program GC.KeepAlive(_mutex); try { return _mutex.WaitOne(0, false); } catch (AbandonedMutexException) { // if one thread acquires a Mutex object //that another thread has abandoned //by exiting without releasing it _mutex.ReleaseMutex(); return _mutex.WaitOne(0, false); } } public Form1() { if (!isSingleInstance()) { MessageBox.Show("Instance already running"); this.Close(); return; } //program body here } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (_mutex != null) { _mutex.ReleaseMutex(); } } 

看看这个问题

这篇文章有一个链接: 误解了互斥锁 ,其中解释了互斥锁的使用。

查看此页面上显示的代码示例

简而言之,您使用过载Mutex ctor(bool, string, out bool)通过out参数告诉您,您是否拥有命名互斥锁的所有权。 如果你是第一个实例,那么在调用ctor之后,这个out参数将包含true – 在这种情况下你会照常进行。 如果此参数为false,则表示另一个实例已经拥有/正在运行,在这种情况下,您将显示错误消息“另一个实例已在运行”。 然后优雅地退出。

使用具有超时和安全设置的应用。 我使用了我的自定义类:

 private class SingleAppMutexControl : IDisposable { private readonly Mutex _mutex; private readonly bool _hasHandle; public SingleAppMutexControl(string appGuid, int waitmillisecondsTimeout = 5000) { bool createdNew; var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); var securitySettings = new MutexSecurity(); securitySettings.AddAccessRule(allowEveryoneRule); _mutex = new Mutex(false, "Global\\" + appGuid, out createdNew, securitySettings); _hasHandle = false; try { _hasHandle = _mutex.WaitOne(waitmillisecondsTimeout, false); if (_hasHandle == false) throw new System.TimeoutException(); } catch (AbandonedMutexException) { _hasHandle = true; } } public void Dispose() { if (_mutex != null) { if (_hasHandle) _mutex.ReleaseMutex(); _mutex.Dispose(); } } } 

并使用它:

  private static void Main(string[] args) { try { const string appguid = "{xxxxxxxx-xxxxxxxx}"; using (new SingleAppMutexControl(appguid)) { Console.ReadLine(); } } catch (System.TimeoutException) { Log.Warn("Application already runned"); } catch (Exception ex) { Log.Fatal(ex, "Fatal Error on running"); } } 

还有这个。 是否使用Mutex来防止同一程序的多个实例运行安全?