两个方法禁止C#窗体程序重复运行

很多时候都需要用到这个功能,避免程序重复运行造成某些错误。

方法一:修改窗体程序的Program.cs内容如下

using System;
using System.Collections.Generic;
using System.Windows.Forms;
      
using System.Diagnostics;//process
using System.Runtime.InteropServices;
using System.Reflection;
namespace 一个实例二
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process instance = RunningInstance();
            if (instance == null)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                HandleRunningInstance(instance);
            }
        }
      
        #region   只运行一个实例
        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();//当前新启动的线程
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            //遍历与当前进程名称相同的进程列表 
            foreach (Process process in processes)
            {
                //process,原来旧的线程与当前新启动的线程ID不一样
                //Ignore the current process 
                if (process.Id != current.Id)
                {
                    //Make sure that the process is running from the exe file. 
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName)
                    {
                        //Return the other process instance. 
                        return process;//返回原来旧线程的窗体
                    }
                }
            }
            return null;
        }
      
        private static void HandleRunningInstance(Process instance)
        {
            MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //调用api函数,正常显示窗口 
            SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。 
        }
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(System.IntPtr hWnd);
        private const int WS_SHOWNORMAL = 1;
        #endregion
    }
}

方法二:同样是修改窗体程序的Program.cs内容如下


using System;
using System.Collections.Generic;
using System.Windows.Forms;
      
namespace 一个实例
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "luyugao_one_test_run", out runone);
            //luyugao_one_test_run为自定义内容,可以换成任意的标识性内容
            if (runone)
            {
                run.ReleaseMutex();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("不允许重复运行");
            }
        }
    }
}

大家使用的时候随意选择即可。效果截图

C#禁止重复运行