winform防止程序重复运行

修改程序主入口函数:

static class Program
{
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
    bool isRuned;
    System.Threading.Mutex mutex = new System.Threading.Mutex(true, "BC064A223AB84B628D03E73631E55145", out isRuned);
    if (isRuned)
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new MainForm());

      mutex.ReleaseMutex();
    }
    else
    {
      MessageBox.Show("程序已启动,请勿重复打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
  }
}

posted @ 2017-11-29 09:45  都是城市惹的祸  阅读(174)  评论(0)    收藏  举报