Check for Single Instance of Application

Abhi2434
Posted by Abhi2434 under Windows Forms category on | Views : 2033
It is very easy to disallow one to open the same application twice. See the code which I implemented :

        [STAThread]
public static void Main()
{
bool instanceCountOne = false;
Mutex mutex = new Mutex(true, "YOURAPPNAME", out instanceCountOne);
try
{
using (mutex)
{
if (instanceCountOne)
{
//Start your application here.
}

else
MessageBox.Show("The application is already running");
}
}
catch { }
finally
{
try { mutex.ReleaseMutex(); }
catch { }
}

}


This is easy, which checks the instancecount using Mutex object. The object will show you how many processes with the same name is running. Thus if you place this code in your main subroutine, it will prevent you to open the same application twice.

Comments or Responses

Login to post response