Multiple Instances of an App

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am running an application in the background. I need to be able to see when another instance of that app is opened, and close the first instance of the app. Anyone know how to do that? I'd appreciate any information

Thanks
Susan
 
The best way I know of is:

Attempt to Create a named mutex. If it succeeds, there is no existing app.
If it fails, then there is an app.

At that point, many people recommend looking for the app by process name and
then killing the process but there's a problem with that. You can never
really be sure that no other app on the machine is using the same name even
though it's a different application.

You do want to use EnumWindows from the Win32API to get applications that
match your app name but then the safest way to know you're killing the
correct application is to use the SendMessage API function to send a message
to the existing app.

If the existing app is your app, and you program it to do so, it will
recognize the message you send and shut down. If it is a similarly named
app, but not your app, then it will not recognize the message and will
discard it.

Hope that helps.

Dale


Susan said:
I am running an application in the background. I need to be able to see
when another instance of that app is opened, and close the first instance of
the app. Anyone know how to do that? I'd appreciate any information.
 
Dale,

A slight suggestion here; instead of using EnumWindows you could use
FindWindow or FindWindowEx API:s from Win32. This way you do not
have to manually enumerate all windows.

HTH,

//Andreas
 
One more point just occurred to me. Normally when you look for existing
instances the first instance wins and therefore the running instance (the
first instance) always holds the mutex.

In your case, because you want the last instance to win, it will have to use
the mutex.WaitOne() method to block the second instance's thread until the
mutex owner (the first instance) relinquishes the mutex.

So, send the message from the second instance to the first, as described
before, then, in the second instance, call mutex.WaitOne(). When the first
instance either ends or calls ReleaseMutex() the second instance will get
ownership of the mutex.... And will own it until another instance is started
and the whole thing happens again, huh?

Good luck,

Dale
 
Thanks for all the info. I will try it first thing in the morning. Where can I find more information to creating a mutex? I am very new to C#, and sorry to say, but most of this is over my head. Again, thanks for all of your help.
 
Here's a C# example of using a mutex taken out of one of my apps.
Unfortunately, I haven't done the SendMessage in C# but if you wish I could
send you some VB samples on it. Or, for VB, you can look at the samples at
MVPS.org, which is where I first got the idea. For more on using Mutex
objects in C# or .Net in general, I'd suggest the Framework documentation.

The example reminds me of another issue. Make sure your mutex name is taken
from a GUID so you are certain you don't end up killing the wrong
application.

Hope it helps,

Dale

[STAThread]

static void Main()

{

bool requestOwnership = true;

bool mutexWasCreated;

Mutex m = new Mutex(requestOwnership,

"0F23BF39FA1247d7949410536EAC8244",

out mutexWasCreated);

if (!mutexWasCreated)

{

return;

}

Application.Run(new DpCForm());

m.ReleaseMutex();

m.Close();

Application.Exit();

}



Susan said:
Thanks for all the info. I will try it first thing in the morning. Where
can I find more information to creating a mutex? I am very new to C#, and
sorry to say, but most of this is over my head. Again, thanks for all of
your help.
 
Back
Top