Single Application Instance Forcing, and subsequent message sending

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;

}
 
D

Dmitriy Lapshin [C# / .NET MVP]

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.
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top