best C#:how to-single instance application that accepts new parameters?

c# getter setter best practices (7)

I'm creating a (C#) program that downloads binaries using NZB files, there may only be one instance of my application running at any time.

So when a user doubleclicks an .nzb-file and my program is not running, it should start and process it (easy, file registration). Now if my program is already running, I do NOT want to launch a second instance - I want the already-running instance to pick up the specified file.

Making my app single-instance can be done using the Visual Basic DLL with the .IsSingleInstance trick, but I don't want to go there.

The right way seems to be to use a mutex to ensure my app is single-instance, but now I'm stuck on how to pass the specified parameter (the .nzb file) to the already-running instance.

Help would be appreciated ! :-)

Look at the InitialInstanceActivator at the Genghis project

One way to do it would be to have program.exe host a WCF service (as you seem to have guessed from your tag). Then, when another instance is started, with command-line arguments, it would be a WCF client to that service, and send the arguments to the service, then exit.

Try this: Your main program creates a named pipe or other interprocess communication facility and listens on it. You create a separate small program that, when run with an NZB file as a parameter, opens the pipe, feeds the file info into it, and exits. Your main program then processes the new file and downloads it.

Why not have one program that adds the file to a queue, and then kicks off the downloading program if it is not already running. That downloading program watches the queue, which is just a file that you append download file names to.

C# application close the first instance of itself

I think you are a little bit confused about the logic yourself. By the way here are some items you should use to do such a thing:

1- System.Diagnostics.Process.WaitForExit

With calling this method for a specific process instance, you can wait until the process is exited completely after you called the "Kill" or "Shutdown" method. It will close the main window in the process if it's a windows app. For the applications which have some confirmation boxes for exiting, it will wait until user confirm or cancel the dialog box (Like when your windows is shutting down while your applications are already open).

2- System.Diagnostics.Process.ExitCode

If you want to access the result of the exist message you send to a process, you should call the value of this property after calling "WaitForExit" function. I guess you can use this property to find out if the first process exited or not and with what reason from the second process.

3- public static int Main

Because you need to access the exit code from the outside of the process you should change the return value of "Main" method to int. This way you can return a value in the first process and access it in the second process through the "ExitCode" property. If returning a value in the "Main" function is difficult in your situation, you can pass the exit code by calling the 4th item.

4- System.Windows.Application.Shutdown (Exit in .NET < 4)

Calling this method does the exact thing as returning the value in the "Main" function.

5- System.Diagnostics.Process.GetProcessesByName

After all by calling this static method you can get all the processes by specified name.

Hope it helps. I just tried to explain everything as simple as possible. These all are the tools you can use to implement the logic. The rest would be so simple if you know these abilities that .NET provided for you.

After all, as I've already forgot to say, if you are interested in doing the right thing, in my opinion and as my experience says, there is a technology that has been made just for these kind of logics.

6- .NET Interprocess Communications or IPC (a sample on code) (introduction on codeguru)

IPC is based on the .NET remoting (the most interesting discussion in .NET in my personal opinion) BUT regardless of what the name says, it's not only for remote communications. The remoting has many sections and on of those is the Inter-Process-Communication (IPC). It's the hard, but the best way if you ask me.