C#中的Mutex类是什么?
C#中的Mutex类是一个同步原语,也可以用于进程间同步。
让我们看看如何创建一个新的Mutex。
private static Mutex m = new Mutex();
现在让我们看看如何使用布尔值初始化Mutex类的新实例。
private static Mutex m = new Mutex(true);
现在让我们看看如何使用布尔值和Mutex的名称初始化Mutex类的新实例。
示例
using System;
using System.Threading;
public class Demo {
public static void Main() {
Mutex mt = new Mutex(false, "NewMutex");
Console.WriteLine("等待互斥。");
mt.WaitOne();
Console.WriteLine("Owner of the mutex. " + "ENTER is to be pressed to release the mutexand exit.");
Console.ReadLine();
mt.ReleaseMutex();
}
}输出结果
等待互斥。 Owner of the mutex. ENTER is to be pressed to release the mutex and exit.
热门推荐
2 敏字的祝福语简短
10 员工对同事祝福语简短
11 简短正能量生日祝福语
12 薛之谦祝福语简短
13 生日祝福语简短蛋糕上
14 2026猪年祝福语简短语
15 给男生的简短祝福语
16 英文写结婚祝福语简短
17 学生毕业季祝福语简短
18 团体年会祝福语简短霸气