mutex

是程序运行的入口点:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(
false);
    Application.Run(
new Form1());
}


单实例限制代码:

[STAThread]
static void Main()
{
    
bool isAppRunning = false;
    System.Threading.Mutex mutex 
= new System.Threading.Mutex(true,  System.Diagnostics.Process.GetCurrentProcess().ProcessName,  out isAppRunning);
    
if (!isAppRunning)
    {
        MessageBox.Show(
"本程序已经在运行了,请不要重复运行!");
        Environment.Exit(
1);
    }
    
else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(
false);
        Application.Run(
new Form1());
    }
}
原文地址:https://www.cnblogs.com/greencolor/p/1567290.html