Andrew Beaton :: FAQ

Tag: Mutex

Run only one instance of a program in C#

by Andrew on Nov.13, 2009, under C#, Programming

This example shows you how to ensure that only one instance of an application is running.

// Ensure only one instance of this application is running.
bool alone = false;
Mutex m = new Mutex(true, "<name>", out alone);
 
if (!alone)
{
    Console.WriteLine("Another instance is already running.");
    Console.ReadKey();
    return;
}
 
Console.WriteLine("Running...\n");
Console.ReadKey();
 
// Needed to ensure only one instance is running.
GC.KeepAlive(m);
1 Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

 

Archives

All entries, chronologically...