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.
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志