C# 创建唯一实例后使用Application.Restart()
使用如下方法保证程序启动唯一实例:
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);  
if (ret)
{
                 Application.EnableVisualStyles();
                 Application.SetCompatibleTextRenderingDefault(false);
                 Application.Run(new FrmMain());                  
                 //Main为你程序的主窗体,如果是控制台程序不用这句    
                 mutex.ReleaseMutex();
  }
  else
  {
      MessageBox.Show(null, "本程序已经运行", Application.ProductName,      MessageBoxButtons.OK,MessageBoxIcon.Warning);
                 Application.Exit();
}
问题:
我想重启程序。
我在代码中使用Application.Restart()后,会提示本程序已经运行。
怎样启用唯一实例,又可以实现重新启动程序的功能???
------解决方案--------------------
class Program
{
  public static System.Threading.Mutex mutex;
  static void Main()
  {
      //...
  }
}
ps:
把System.Threading.Mutex mutex写成一个静态成员后,就不用在Main()中mutex.ReleaseMutex();  
而关闭Mutex可以直接用Program.mutex.Close(); 这里它跟Dispose的作用一样。