Reputation: 41387
In .NET, what is the most reliable way to detect if an instance of the same program is already running? Further, what is the best way to pass command line arguments to the already-running instance (e.g. to open a specific document)?
Upvotes: 2
Views: 2686
Reputation: 3789
To determine if an application is running, don't use Mutex because it has a huge overhead. Use
using System.Diagnostics;
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses) {
if (clsProcess.ProcessName.Contains(name)) return true;
}
return false;
}
The more information you give about this "other application" the better. If you developed the "other application", you could serialize some data to the other application, to let it know what other information you want it to pick up. Check out the .NET framework XMLSerializer. You could even do something simple as using a database entry. You can use a UDP packet. There are many ways to communicate to another program. The question is, what is the other program?
Upvotes: -1
Reputation: 46833
"what is the best way to pass command line arguments to the already-running instance (e.g. to open a specific document)?
Unfortunately I don't believe this is possible. The application could use Console.ReadLine() to get input from Standard Input, but this is not the same as passing in command line arguments.
There are several other ways for Interprocess communication via .NET. .NET Remoting and WCF, as well as your own custom socket protocols.
You can use the System.Diagnostics namespace to gather a list of running processes.
Upvotes: 0
Reputation: 134005
There appears to be three well-known ways of implementing single-instance applications in .NET programs: the process method, the Mutex method, and the Visual Basic method.
Michael Covington discusses the first two in his hint, Ensuring that only a single instance of a .NET application is running.
There's a CodeProject article that discusses the Mutex method in more detail, including getting the window handle of the existing application so that you can then (somehow) communicate with it.
Another CodeProject article describes how to use classes in the Microsoft.VisualBasic namespace (which is accessible from C#) to implement a single-instance application.
I'm not at all familiar with the third method. Of the first two, they both work but as is pointed out in the linked articles, the Mutex method seems more robust. I don't know why Mutex was selected as the named object to use. Any of the named synchronization objects (EventWaitHandle, Mutex, Semaphore, etc.) would work.
Communicating with the existing application presents more of a problem. I'd suggest you look into using named pipes, if all you need to do is send a relatively small message to the running application. Check out System.IO.Pipes.NamedPipeClientStream for more information.
I should also note that if you're worried about security, you'll need to secure whatever communications method you use. Any application that has sufficient permissions will be able to send information to your running application through the named pipe.
Upvotes: 3
Reputation: 13696
You may use .Net remoting(.Net 2.0) or WCF(.Net 3.5) to communicate between programs on the same or different computers.
You can't pass command line arguments to already running program. You can pass it any data you want using any method you want, but it will have to deal with this data consciously. It won't magically be used as command line arguments
Upvotes: 1
Reputation: 570
I don't know if this is the best way. I use a singleton object that basically is just a place to hold a named mutex. If the application tries to run twice it checks to see if the mutex is in use and if so it stops execution so it can't run twice.
I'm not sure how to talk to the already running application, however.
Upvotes: 0