[STAThread]
static void Main()
{
Process Instance = RunningInstance();
if (Instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("条码打印程序已经在运行,请勿多开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ActiveForm(Instance);
}
}
private static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] ProcessList = Process.GetProcessesByName(current.ProcessName);
for (int i = 0; i < ProcessList.Length; i++)
{
if (ProcessList[i].Id != current.Id)
{
if (ProcessList[i].MainModule.FileName == current.MainModule.FileName)
{
return ProcessList[i];
}
}
}
return null;
}
本文介绍了一个简单的C#程序,用于确保应用程序在同一时刻只能有一个实例运行。通过比较当前进程与其他正在运行的实例,程序能够判断自身是否为唯一运行的实例,并在尝试启动第二个实例时给出警告。

2185

被折叠的 条评论
为什么被折叠?



