Monday, January 14, 2008

Ensure Single Instance of Apps using Mutex

An example of making ensuring your application is single instance using Mutex. Same can be done using the Process Name, but i found "Mutex" approach much better.

static void Main(string[] args)
{
bool bExists;
Mutex objMutex = new Mutex(false,"TestApp",out bExists);
if(bExists)
{
while(true)
{
Console.WriteLine("Hello");
}
}
else
{
Console.WriteLine("An Instance of application already exists");
Console.ReadLine();
}
}