Single Application Instance Forcing, and subsequent message sending

  • Thread starter Thread starter Jonathan Rea
  • Start date Start date
J

Jonathan Rea

Hi,

I have found the code below to detect if a process is already running. This
seems to work well, but then how do you sent the main program class in
process 1 messages about the command parameter info of process 2.

Thanks,
Jon

private static Process RunningInstance()

{

Process current = Process.GetCurrentProcess();

Process[] processes = Process.GetProcessesByName (current.ProcessName);

//Loop through the running processes in with the same name

foreach (Process process in processes)

{

//Ignore the current process

if (process.Id != current.Id)

{

//Make sure that the process is running from the exe file.

if (Assembly.GetExecutingAssembly().Location.

Replace("/", "\\") == current.MainModule.FileName)


{

//Return the other process instance.

return process;


}

}

}

//No other instance was found, return null.

return null;

}
 
Hi Jonathan,

You can for example start the second instance silently (without showing any
windows), read the command line args, pre-process them and, say, post a
user-defined Windows message to the first instance's message queue.
 
Jonathan Rea said:
I have found the code below to detect if a process is already running. This
seems to work well, but then how do you sent the main program class in
process 1 messages about the command parameter info of process 2.

Dmitry has given you an answer to the second part of your question (you
could use sockets as well), but I'd suggest a different way of
detecting an existing process - use a Mutex. See
http://www.pobox.com/~skeet/csharp/faq/#one.application.instance
 
Back
Top