winform 判断程序是否已经启动,防止重复打开
Program.cs 文件,应用程序的主入口文件:
C# Code复制内容到剪贴板
- namespace WindowsFormsApplication1
- {
- static class Program
- {
- private static System.Threading.Mutex mutex;
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- mutex = new System.Threading.Mutex(true, "OnlyRun");
- if (mutex.WaitOne(0, false))
- {
- Application.Run(new Form1());
- }
- else
- {
- MessageBox.Show("程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- Application.Exit();
- }
- //Application.Run(new Form1());
- }
- }
- }