Saturday, February 14, 2009

force one instance of an application in .NET

To force a single instance of an application, either fetch all processess and check your application process name that is it already there or not. Another option is to use Mutex class.

bool NoInstanceCurrently;
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "applicationname", out NoInstanceCurrently);
if (NoInstanceCurrently == false)
{

MessageBox.Show("Another Instance is Running", "ApplicationName", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}

No comments: