1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| static Mutex mutex = new Mutex(true, Application.ProductName);
[STAThread] static void Main() { if (mutex.WaitOne(TimeSpan.Zero, true)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); mutex.ReleaseMutex(); } else { MessageBox.Show("应用程序已在运行"); } }
|
WPF
1 2 3 4 5 6 7
| private static Mutex mutex; private void Application_Startup(object sender, StartupEventArgs e) { mutex = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out bool createdNew); if (!createdNew) Shutdown(); }
|
参考资料
https://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-wpf-application