tags:

views:

461

answers:

4

I have an application that provides core services for a series of other applications.

When another of these applications is started, I want to check that the service application is running and if not shutdown.

What is the best method to check for the existence of the other app? I'm thinking that I should be using a global mutex in the services app and checking for it's existence in the other apps. Is this the correct way to proceed?

+3  A: 

The global mutex approach is a good one, and one that many apps use.

For example, Windows Media Player does this.

Related question:

http://stackoverflow.com/questions/646480/is-using-a-mutex-to-prevent-multipule-instances-of-the-same-program-from-running

Martin Peck
+1  A: 
bool IsApplicationAlreadyRunning()
{
string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
return true;
else
return false;
}

Source: http://kseesharp.blogspot.com/2008/11/c-check-if-application-is-already.html

NinethSense
A mutex is a better approach. Using the process name can potentially fail if more than one process has the same name, however unlikely that may be.
Chris Dunaway
A: 

Yes- just be careful what you call the mutex. There are specific naming guidelines for these- this has some details

RichardOD
A: 

I'd say to look into the method you're using for communication with the service app, and look at what the communication failure behaviour is. If you attempt to communicate with the service app at any time and fail, it looks like you want to have failure behaviour, because of the high dependency. If you have a failure at first communication, it seems like you want to exit the application; but you also have to worry about what happens if your communication fails at some later point, too.

McWafflestix