Controlling one instance of the application from another?

M

melon

Let's say I have a program.exe file. When I run it, one instance of
it will be created.

If I run it again, then another instance will be created. Question
is, is it possible for instance #2 to issue command to instance #1, or
vice versa?

Thanks
 
M

Marc Gravell

Via some form of IPC (remoting, WCF, a shared file, sockets, etc etc
etc), yes.

The implementation depends on what you need...

Marc
 
G

Guest

There are many-a-ways to achieve that...
1) You can use Mutex
2) At the program constructor, trace the process that are currently running
on the system, take either name or id of the application and compare with the
current.. application.

If you need more details with code, ping me back..

HTH
 
G

Guest

Using the process as below

First create a method to get the instance is already running or not as below

using System.Diagnostics;

public static Process PriorProcess()
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return p;
}
return null;
}

Now by using this, you can check at the Main method as

static void Main() // args are OK here, of course
{
if (PriorProcess() != null)
{
MessageBox("Another instance is already running.");
return;
}
Application.Run(new Form1()); // or whatever was here
}

Regarding Mutex usage... if you can do R&D that's great. But if you want me
to post the details here will do that...

HTH and don't forget to rate the post
 
M

Marc Gravell

Although "one instance only" is a common request, I'm not sure it is
what the OP asked on this occasion?

Marc
 

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