“mutex”是术语“互相排斥(mutually exclusive)”的简写形式,也就是互斥量。

如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。

ReleaseMutex 方法同样多的次数以释放互斥体的所属权。

即:前缀名称“Global\”和“Local\”说明 mutex 名称相对于终端服务器会话(而并非相对于进程)的范围。

简单示例:

 static class Program     {        

/// <summary>        

/// 应用程序的主入口点。        

/// </summary>

        [STAThread]

        static void Main()

        {         

            bool CreateNew;

            System.Threading.Mutex mutex = new System.Threading.Mutex(false, "Application.ProjectName", out CreateNew);

            if (!CreateNew)

             {                

      System.Windows.Forms.MessageBox.Show("系统已运行,不用重复运行!");

                Application.Exit();

                return;

            }

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new LoginForm());

        }

    }

 

  下面的代码来自https://msdn.microsoft.com/zh-cn/library/system.threading.mutex.aspx

Mutex 对象来同步对受保护资源的访问。

// Release the Mutex.
        mut.ReleaseMutex();
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-11-28
  • 2021-11-11
  • 2021-11-22
  • 2021-12-02
  • 2021-05-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2021-07-09
  • 2021-07-14
  • 2022-12-23
相关资源
相似解决方案