[C#]使用Mutex實現單一程式執行個體的注意事項
namespace WindowsFormsApplication10 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);
Boolean bCreatedNew;
//Create a new mutex using specific mutex name
Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew);
if (bCreatedNew)
Application.Run(new Form1());
}
}
}
//Create a new mutex using specific mutex name
Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew);
GC.Collect();
if (bCreatedNew)
Application.Run(new Form1());
...</pre>
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Boolean bCreatedNew;
//Create a new mutex using specific mutex name
m = new Mutex(false, "myUniqueName", out bCreatedNew);
GC.Collect();
if (bCreatedNew)
Application.Run(new Form1());
}
...</pre>
//Create a new mutex using specific mutex name
Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew);
GC.Collect();
if (bCreatedNew)
Application.Run(new Form1());
m.Dispose();
...</pre>
//Create a new mutex using specific mutex name
using (Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew))
{
GC.Collect();
if (bCreatedNew)
Application.Run(new Form1());
}
...</pre>
//Create a new mutex using specific mutex name
Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew);
m.WaitOne();
GC.Collect();
if (bCreatedNew)
Application.Run(new Form1());
m.ReleaseMutex();
...</pre>