1.

What Is The Meaning Of Mutex?

Answer»

A Mutex is like a C# lock, but it can work across multiple processes. In other words, Mutex can be computer-wide as

well as application-wide. 

With a Mutex class, you call the WaitOne method to lock and ReleaseMutex to unlock. CLOSING or disposing a Mutex automatically releases it. Just as with the lock statement, a Mutex can be released only from the same thread that obtained it. A common USE for a cross-process Mutex is to ensure that only one instance of a program can run at a time. Here’s how it’s done:

class OneAtATimePlease

{

STATIC void Main()

{

// Naming a Mutex makes it AVAILABLE computer-wide. Use a name that's

// unique to your company and application (e.g., include your URL).

using (var mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"))

{

// Wait a few SECONDS if contended, in case another instance

// of the program is still in the process of shutting down.

if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))

{

Console.WriteLine ("Another instance of the app is running. Bye!");

return;

}

RunProgram();

}

}

static void RunProgram()

{

Console.WriteLine ("Running. Press Enter to exit");

Console.ReadLine();

}

A Mutex is like a C# lock, but it can work across multiple processes. In other words, Mutex can be computer-wide as

well as application-wide. 

With a Mutex class, you call the WaitOne method to lock and ReleaseMutex to unlock. Closing or disposing a Mutex automatically releases it. Just as with the lock statement, a Mutex can be released only from the same thread that obtained it. A common use for a cross-process Mutex is to ensure that only one instance of a program can run at a time. Here’s how it’s done:

class OneAtATimePlease

{

static void Main()

{

// Naming a Mutex makes it available computer-wide. Use a name that's

// unique to your company and application (e.g., include your URL).

using (var mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"))

{

// Wait a few seconds if contended, in case another instance

// of the program is still in the process of shutting down.

if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))

{

Console.WriteLine ("Another instance of the app is running. Bye!");

return;

}

RunProgram();

}

}

static void RunProgram()

{

Console.WriteLine ("Running. Press Enter to exit");

Console.ReadLine();

}



Discussion

No Comment Found