There are certain circumstances when you need to allow your application to run just once. Such requirement usually depends on the scenario you're currently working on. One of the most common reasons to limit the number of instances is to restrict the access to some sensitive resources.
For this purpose, we use the Mutex object. This approach can be used on all three types of desktop applications (console, winforms, WPF). A Mutex (mutual exclusion) is an object (or principle if you want) that is commonly used for synchronizing several threads when they are accessing and using the same resources. It is sort of a gate keeper that allows just one visitor to enter the code at a time. You can check WikiPedia to better understand this principle: Mutial exclusion.
Even though the same principle can be used in all three types of desktop applications, there are minor differences among them. To be more specific, you need to find the proper entry point where the Mutex will be used.
Now let me go through each type to show the usage.
Console Application
The entry point of a console application is the Main method. When we run the application, a Mutex object with a unique identifier (the appName in this case) is created. The constructor takes 3 arguments, with the 3rd being the most important. This output parameter simply tells us whether other objects with the same identifier was already created. If so, other instances of our application is already running, so we need to close the current instance.
class Program
{
private static Mutex mutex = null;
static void Main(string[] args)
{
const string appName = "MyAppName";
bool createdNew;
mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
Console.WriteLine(appName + " is already running! Exiting the application.");
Console.ReadKey();
return;
}
Console.WriteLine("Continuing with the application");
Console.ReadKey();
}
}
Windows Forms Applications
In Windows Forms applications, the approach is nearly the same. The entry point of the application is usually the Main method in the static Program class.
[STAThread]
private static Mutex mutex = null;
static void Main()
{
const string appName = "MyAppName";
bool createdNew;
mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
//app is already running! Exiting the application
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
WPF
Using a Mutex in WPF is a little trickier, but still easily achievable. The entry point of a WPF app is the App.xml.cs (a code behind file of App.xaml) and we need to override the OnStartup method to properly exit the application right after the start if needed.
public partial class App : Application
{
private static Mutex _mutex = null;
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "MyAppName";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
//app is already running! Exiting the application
Application.Current.Shutdown();
}
base.OnStartup(e);
}
}
I hope you enjoyed this brief Mutex usage introduction. Comments are more than welcome.

Syed FaizunnabiPosted Jul 31, 2019, 2:35 AM
Very useful..thanks for sharing
Farid HeydarovPosted Sep 26, 2018, 2:55 AM
Very useful. Thank you very much !
Lê HùngPosted May 29, 2018, 9:36 PM
Thanks you very much
kinjal patelPosted Mar 31, 2017, 4:45 AM
Thank you for support
ankur guptaPosted Jun 4, 2016, 3:33 AM
You can find a very simple and working impementation for the same here http://getgoingit.blogspot.in/2016/06/restrict-desktop-application-to-single.html
Stu KayPosted Apr 13, 2016, 10:56 AM
When using the Windows Forms example I found the 'private static mutex...' line needed to be placed before the [STAThread] line otherwise I get the following error message: Attribute 'STAThread' is not valid on this declaration type. It is only valid on 'method' declarations.
Ashraf AnsariPosted Dec 22, 2014, 4:20 AM
good one, zabardast
Lea MangaoangPosted Dec 11, 2014, 8:41 PM
nice article... just the one i was looking for... thanks
Joginder BangerPosted Dec 2, 2014, 2:02 AM
Good job michal
Rahul Kumar SaxenaPosted Dec 2, 2014, 2:02 AM
Very informative Michal
Dinesh BeniwalPosted Dec 1, 2014, 11:17 PM
Good work Michal.
Sam HobbsPosted Dec 1, 2014, 10:51 PM
Thank you Michal for this useful article. One thing to add is that since there is a slight chance that the mutex could go out of scope and get garbage collected, it would help to either make the mutex static or call GC.KeepAlive(mutex) at the bottom of the method.
Mahesh ChandPosted Dec 1, 2014, 3:01 PM
Nice Michal. Welcome to C# Corner.