คลาส Mutex ใน C# เป็นพื้นฐานการซิงโครไนซ์ที่สามารถใช้สำหรับการซิงโครไนซ์ระหว่างกระบวนการได้
ให้เราดูวิธีสร้าง 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("Waiting for the Mutex.");
mt.WaitOne();
Console.WriteLine("Owner of the mutex. " + "ENTER is to be pressed to release the mutexand exit.");
Console.ReadLine();
mt.ReleaseMutex();
}
} ผลลัพธ์
Waiting for the Mutex. Owner of the mutex. ENTER is to be pressed to release the mutex and exit.