Monday, February 20, 2012

using Mutex in threads instead of thread lock

Use the below sample in a console application and run it as 2 exe, to understand mutex

Mutex m = new Mutex(false, "MyMutex");

// Try to gain control of the named mutex. If the mutex is
// controlled by another thread, wait for it to be released.
Console.WriteLine("Waiting for the Mutex.");
m.WaitOne();

// Keep control of the mutex until the user presses
// ENTER.
Console.WriteLine("This application owns the mutex. " +
"Press ENTER to release the mutex and exit.");
Console.ReadLine();

m.ReleaseMutex();

No comments: