views:

137

answers:

4

In C#, I know how to run a .NET executable from code and also find out if an instance of the executable is already running. What I would like to do is if an instance is already running, obtain an instance of the Foo object within the C# code of a different executable.

I have a windows application, (e.g..NET version of Outlook). A user can use the application as normal, but also running in the background is a process watching for an XML file from a third party system.

What I need is for the watcher process to start the .NET program if it is not running (or obtain a handle if it is), and then call the method CreateEmail on the object instance within the new/existing process.

+1  A: 

Use the System.Diagnostics.Process class.

To run, you can Process.Start("string path"); and also list current running processes to perform the check.

Kyle Rozendo
Thanks for the reply, but as I had said:"I know how to run a .NET executable (foo.exe) from code and also find out if an instance of the executable is already running"
Philip Wallace
Misread, thought it was "need to know", apologies.
Kyle Rozendo
Your title says "How to start a .NET process...", so perhaps that should be removed.
Ed Swangren
Done. Hopefully it's a bit clearer.
Philip Wallace
A: 

Well, you can use:

Process.GetProcessesByName(<process Name here>);

which returns Process[].

Check out the static methods of the Process class, they are very intuitive and helpful.

Vitaliy
Thanks for the reply, but as I had said: "I know how to run a .NET executable (foo.exe) from code and also find out if an instance of the executable is already running"
Philip Wallace
+2  A: 

You can activate an object in an already running application using .NET Remoting.

Check out the following sample: About .NET Remoting: Three concepts Compared and Simplified

0xA3
Excellent idea. Thank you!
Philip Wallace
Remoting is deprecated because it allows processes to fiddle around with each other's internals. This is brittle and can lead to nasty surprises (as you can't control what the external process does to your objects). A better way would be for the target process to host a WCF service: because this is a message-based interface, it insulates the calling process from the implementation of the target process (allowing change) and allows the target process to control what is done to its objects and when (preventing surprises).
itowlson
WCF sounds like a good option - I'll look into it.
Philip Wallace
+1  A: 

Why don't you just added the FileSystemWatcher to the main application? That's if the background process is doing nothing else but monitoring for the XML files.

If that's not feasible, you could use the NamedPipeServerStream and NamedPipeClientStream to send a "command" from the background process to the main application. When the main application receives this command, it will run the CreateEmail method.

Eclipsed4utoo
Or use WCF over a named pipe channel -- saves writing your own protocol down at the byte stream level.
itowlson
as of .Net 3.5, .Net now supports named pipes. No writing your own protocol. The NamedPipeServerStream and NamedPipeClientStream are very easy to use for IPC. I did a blog post about it a few weeks ago. http://eclipsed4utoo.com/blog/completely-managed-interprocess-communication/
Eclipsed4utoo
Consider the main application as signed and sealed. The watcher is a customer specific tool so I don't want it integrated into the main source code.
Philip Wallace
After some quick tests, it seems that using WCF over a named pipe channel gives me exactly what I need. Thanks to Eclipsed4utoo and itowlson for the suggestions!
Philip Wallace