views:

142

answers:

4

How can I make my already running C# Windows Form Application be able to receive commands from the command line while it is already running?

For example: if my application is playing a video now then I want to be able to type on the command line "MyApp /stop" so that while the application is still running it stops the playing the video without exiting from current session.

+2  A: 

By sending a command like that, you'd be firing up another process. Certain command line arguments could do some kind of IPC to signal the "main" instance of the running app.

Larsenal
+4  A: 

From your question it seems that your first process is still running and you start a second instance of it, and you wish that instance to communicate with the first.

What you are looking for is called inter-process communication (IPC). The standard way of doing this in .NET is to use Windows Communication Foundation (WCF).

Mark Byers
You might sometimes see IPC referred to as named pipes. There's a pretty good walkthrough here: http://omegacoder.com/?p=101
fatcat1111
Named pipes are *one* form of IPC. There are others, some of which may be better-suited for this task.
Steven Sudit
+2  A: 

One way would be to make your app a singleton, and whenever another instance is run, it will pass arguments to the already running process.

Example: http://www.codeproject.com/KB/cs/SingletonApplication.aspx

Am
This link seems to be matching what i need, Thanks.
CSharpBeginner
+1  A: 

Without changing your design structure and assuming your application is a standalone application (running on a local PC),

One method is to make one thread of your application wait (WaitOne()) for a named mutex or semaphore (link text).

When you start your application (your second instance), you parse your commandline (via the args arguments). If the args[0] contains your "/stop" command, you "Release()" the named mutex/semaphore. Then your thread (in the first instance) will be waken to stop the playing the video.

Then again (having said all the above), a more simple solution is to have a STOP button in your application where the user can click on it.

Syd