【C#】 Debug运行文件只能运行一个(不能打开多个)

来源:互联网 发布:唯一网络被收购 编辑:程序博客网 时间:2024/06/05 01:03

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);

   }

}



阅读全文
0 0
原创粉丝点击