首先引用System.Diagnostics。
using System.Diagnostics;
private static void GetSingleThread()
{
string name = Process.GetCurrentProcess().ProcessName;
int id = Process.GetCurrentProcess().Id;
Process[] prc = Process.GetProcesses();
foreach(Process pr in prc)
{
if ((name == pr.ProcessName) && (pr.Id != id))
{
MessageBox.Show("对不起,本地已经有系统正在运行!/n.","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
System.Environment.Exit(0);
}
}
}
之后在form的初始化时加入即可实现。
public Form1()
{
GetSingleThread();
InitializeComponent();
}