I wanted to make sure that only one instance of a program is running on my machine, and when a second instance would be started, it would pass its command line arguments to the original instance and then terminate. Thus, the original instance can handle everything else, such as opening a file, bringing itself to the foreground, etc. Also, the solution should not employ outdated techniques such as DDE, and should not use any unmanaged code as I had seen in so many other solutions to this problem.
Part 1: Many solutions I have seen walk through the process list in order to identify a previous instance. Others use Mutex, which I found appealing as it is a lot faster and is completely managed code. This solution uses the full path of the executing assembly as the Mutex name, so it is definitely unique.
Part 2: Other solutions use DDE to communicate with the previous instance. I chose .NET remoting because, again, it is fully managed code and not a Windows legacy, and it also works with console applications, while DDE would require a window.
The whole functionality is encapsulated in a class, written in C# using .NET 2.0. To make this work with .NET 1.1, you would have to change some namespaces, otherwise it is fully compatible.
To demonstrate the principle, I created a simple console application. Of course, you'd need to enhance the class a little for general use. This example has, for instance, a fixed port number, and certainly other flaws for generic usage. But it should only demonstrate the principle.
Using the Code
The class that handles the whole thing is named
SingletonController, and it has some
static methods that would be used by the calling program (your main program).
The test whether this is the first instance, as you can see above, creates a delegate (callback) function. This function will be called whenever a second instance is opened.
else branch handles the second instance, which will pass its arguments before terminating.
SingletonController class has a couple of building blocks:
- it defines a
ReceiveDelegate, which will be set to the original instance's callback function
IamFirst() function, which returns
true if this is the first instance of your application
CreateInstanceChannel() function, which creates a small remoting listener; this receives the arguments from any subsequent instances
Send() function, which sends all arguments from a second instance to the initial instance before it terminates itself.
public static bool IamFirst()
string assemblyName =
m_UniqueIdentifier = assemblyName.Replace("\\", "_");
m_Mutex = new Mutex(false, m_UniqueIdentifier);
if (m_Mutex.WaitOne(1, true))
//We locked it! We are the first instance!!!
//Not the first instance!!!
m_Mutex = null;
This function creates a Mutex based on the full path name of the executing assembly, then tries to lock it. If successful, it calls
CreateInstanceChannel(), which will create a small remoting listener. This listener is later responsible for calling your main program's callback function.
private static void CreateInstanceChannel()
m_TCPChannel = new TcpChannel(1234);
This function creates the remoting listener. I hard-coded the port as 1234, this is what you want to put into your configuration file.
public static void Send(string s)
TcpChannel channel = new TcpChannel();
ctrl = (SingletonController)Activator.GetObject(
catch (Exception e)
Console.WriteLine("Exception: " + e.Message);
This function needs to be called by your main program in case it is the second instance (
IamFirst() == false). It will send the arguments supplied to the first instance (the remoting listener), which in turn will call your callback function of the first instance. You will notice that this is the only call to a non-static function (
ctrl.Receive()), your own code never needs to instantiate the
Receive() function finally calls your callback using the delegate member:
public void Receive(string s)
if (m_Receive != null)
Running the Sample Code
After you compile the sample code, do this:
- open two command windows.
- run SingletonApp.exe in one of the windows; it will start saying "Hi: 0, Hi: 1, ..." every second and will terminate after 10 iterations.
- within 10 seconds after the above application runs, start SingletonApp.exe in a second command window and supply some command line arguments; watch the first window printing out the arguments supplied in the second window.
If you supply more than one argument, you will notice that the "Hi: 0, Hi: 1, ..." loop and the loop that prints the arguments run in separate threads, the "Hi" messages and arguments will be printed in an alternating fashion.
Points of Interest
None, this solution has been put together from different solutions I had found on the net, none of which combined it the way I wanted it to be. So, this is basically nothing new, just a new combination.
No changes (yet).