new Mutex : Mutex « Thread « C# / C Sharp






new Mutex

 


using System;
using System.Threading;


class NETMutex {
    static Mutex myMutex;

    public static void Main() {
        myMutex = new Mutex(true, "AAA");
        NETMutex nm = new NETMutex();
        Thread t = new Thread(new ThreadStart(nm.Run));
        t.Start();
        Thread.Sleep(5000);
        myMutex.ReleaseMutex();
        myMutex.WaitOne();
    }

    public void Run() {
        Console.WriteLine("In Run");
        myMutex.WaitOne();
        Console.WriteLine("Thread sleeping for 10 secs");
        Thread.Sleep(10000);
        Console.WriteLine("End of Run() method");
    }
}

 








Related examples in the same category

1.new Mutex(true, "MutexExample", out ownsMutex)), ReleaseMutex
2.new Mutex(false), WaitOne