限制程序重复执行

来源:互联网 发布:js实现动态下拉菜单 编辑:程序博客网 时间:2024/04/29 00:29
private void MainWindow_Load(object sender, System.EventArgs e)
{

    Process[] processes=Process.GetProcessesByName("MYEXENAME");
      if( processes != null)
     {
    if( (processes.Length)== 2)  //第2个进程
    {
       processes[1].CloseMainWindow();

      }
   }
}

 

STAThread]
        static void Main(string[] args)
        {
            //保证该程序只有一个在运行
            bool createdNew;
            System.Threading.Mutex mutex_Application = new System.Threading.Mutex(true,"test",out createdNew);
            if (!createdNew)
            {
                MessageBox.Show("本程序只允许同时运行一个!");
                return;
            }
        
            Application.Run();
            
        }        

原创粉丝点击