C#运行软件的时候,同一Debug可以同时运行多个,,这样就会对项目有影响,直接上代码!
using System.Threading;
using System.Reflection;
privatestaticSemaphore singleInstanceWatcher;
privatestaticbool createdNew;
public MainWindow()
{
// 确保不存在程序的其他实例
singleInstanceWatcher = newSemaphore(
0,// Initial count.
1,// Maximum count.
Assembly.GetExecutingAssembly().GetName().Name,out createdNew);
if (createdNew)//没有运行程序
{
InitializeComponent();
}
else//已经运行过一次
{
MessageBox.Show("程序已在运行中","提示信息");
Environment.Exit(-2);
}
}