Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
what is mutex In C#.NET or how to use this as a class , method or property
Posted
Updated 3-Sep-18 20:29pm

Really, is typing "google" too much of a strain for you? A mutex is a synchronization object used in multithreaded apps.

If google "C# what is a mutex", you'll get 109,000 results returned. I bet you'll get a more detailed answer from one of those.
 
Share this answer
 
Here are some good articles on how to use it:


A Pure .NET Single Instance Application Solution[^]

Mutex Process Identifier[^]
 
Share this answer
 
Mutex stands for mutual exclusion. The C# language provides the Mutex type to ensure that certain blocks of code are executed only once at a time. The Mutex type can create named mutexes that can be opened by other processes. It is useful for ensuring that only one instance can be run on the computer.

Example
This simple example uses the Mutex.OpenExisting method. If the OpenExisting method throws an exception, the specified named Mutex does not exist or is inaccessible. The IsSingleInstance method uses this behavior.

If the Mutex does not exist, it creates a new one. Further instances of the program then can tell that there is an instance already open.

Program that uses Mutex and OpenExisting [C#]

using System;
using System.Threading;

class Program
{
static Mutex _m;

static bool IsSingleInstance()
{
try
{
// Try to open existing mutex.
Mutex.OpenExisting("PERL");
}
catch
{
// If exception occurred, there is no such mutex.
Program._m = new Mutex(true, "PERL");

// Only one instance.
return true;
}
// More than one instance.
return false;
}

static void Main()
{
if (!Program.IsSingleInstance())
{
Console.WriteLine("More than one instance"); // Exit program.
}
else
{
Console.WriteLine("One instance"); // Continue with program.
}
// Stay open.
Console.ReadLine();
}
}

Result

1. First execution will display "One instance"
2. Second execution will display "More than one instance"
3. Third execution is the same as second.

Performance notes. The program unfortunately incurs one exception in the common case of the first instance being run. Exceptions are measurably slower than many constructs. However, if your program is computationally-intensive, it would be more beneficial to avoid extra instances being started.

Please note:
The OpenExisting examples on MSDN also use exception handling.

MSDN Reference [Mutex.OpenExisting]
Methods

It is sometimes important to call Release on your Mutex instance once you are done with it. In the simple example above this is not necessary as the Mutex will be released when the program exits. The code works well without releasing the Mutex.

Continuing on, the Mutex type provides the WaitOne, WaitAll and WaitAny methods. The WaitOne method blocks the current thread until the Mutex has been released. Releases occur when the Release method is called.

Summary

The Mutex type provides a way to use system-wide synchronization. It can also synchronize threads within a single program.

Tip:
When a Mutex has a string name, it can be opened from other processes with OpenExisting. This behavior can be used to enforce synchronization between processes and threads.


Hope this will help you guys.....
-avb
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900