方式一:
获取GUID:
using System;
using System.Threading;
using System.Windows.Forms;
namespace MVCFormApp
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Mutex mutex = new Mutex(true, "{ 87475B1D - 6752 - 478A - B82B - 31938AB96358}", out bool createNew);
if (!createNew)
{
MessageBox.Show($"{Application.ProductName} 已经在运行","提示");
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
二、方式二:
using System;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
namespace MVCFormApp
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (processes.Length > 1)
{
/*
* MessageBoxButtons.OK 表示消息框会显示一个 "确定" 按钮 (OK),用户可以点击该按钮关闭消息框。
* MessageBoxIcon.Information 指定显示一个信息图标,通常是一个带有字母 "i" 的圆圈图标
*/
MessageBox.Show("此程序已经在运行","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Thread.Sleep(1000);
Environment.Exit(1);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}