Limit multiple launches of same app

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

I have a small application that when launched I want it to
prevent another instance of itself being launched. I
tried singleton design, but it only limits the creation of
one instance of an object within an application. I want to
prevent a user from launching the application, then while
it is running launching it again. When they try to launch
the exe again, I only want the existing process to be
given focus instead of launching a whole new process. I
have the app checking for more than 1 process running, but
it still brings up a console window momentarily. Thanks
in advance. . .
 
JJ,

You can do what you want pretty easily using the Mutex class. Here's a clip
from a WinForm app that I use:
 
*ach* this time with code...

static void Main()
{
string mutexName = "Some.Unique.String.Name";
using(Mutex instanceMutex = new Mutex(false, mutexName))
{
if(instanceMutex.WaitOne(1, true) == false)
{
MessageBox.Show("Another instance of FileEye is running.", "Multiple
Instance Alert");
return;
}
frmMain mf = new frmMain();
// clip ......
}
}
 
Back
Top