C#禁止一個程序多次打開

方案一:
//判斷是否已經存在一個exe  
  是要寫在main函數裏面的  
  [STAThread]  
  static   void   Main()    
  {  
  bool   createdNew;  
  Mutex   m   =   new   Mutex(true,   "yourexe",   out   createdNew);  
  if   (!   createdNew)  
  {  
  MessageBox.Show("Only   one   exe   is   allowed   at   a   time.");  
  return;  
  }  
  Application.Run(new   Start());  
  GC.KeepAlive(m);  
  }  
   
  上面的要添加using   System.Threading;  
  Mutex   class是用於進程同步的


方案二:
using   System.Diagnostics;  
   
  ///   <summary>  
  ///   應用程序的主入口點。  
  ///   </summary>  
  [STAThread]  
  static   void   Main()    
  {  
  //   防止多次執行  
  Process[]   processes   =   Process.GetProcessesByName("RCL");  
  if   (processes.Length   >=   2   )          
  {  
  MessageBox.Show("程序已經執行!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);  
  return;  
  }  
  else  
  Application.Run(new   frmMain());  
  } 

發佈了159 篇原創文章 · 獲贊 3 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章