9

is it possible to run method in new child process? In my example I can execute method Run, which will execute private method doAction in new process (not thread!)

public class MyClass
{
    public void Run()
    {
        //what should I do there to run 'doAction' in new process?
        doAction();

    }

    private void doAction()
    {
        ...
    }
}
5
  • 2
    Why would Thread not be sufficient? Why would you ever need 2 myprogram.exe's running at once, just to run a method? (if that's even possible.)
    – ddavison
    May 3, 2013 at 19:05
  • My question is rather curiosity than real problem. I want to know is it possible
    – Jacek
    May 3, 2013 at 19:07
  • To better understand this problem I think we need to better understand your overall way of thinking -tell us what you're trying to achieve (not technically but from a function perspective) so we can better understand. May 3, 2013 at 19:07
  • 1
    A reason to do this would be to execute a long running method not capable of cooperative termination. Jan 29, 2014 at 5:52
  • 1
    @sircapsalot Another reason to use a second myprogram.exe is if doAction needs to be executed in a process started with "Run as administrator." Jan 23, 2015 at 9:54

1 Answer 1

0

Processes do not share memory and require a mechanism between them to communicate. You can use one of a variety mechanisms for this:

  1. File or data - one process writes to a file or DB, the other reads from and executes some method based on the data or file content
  2. Service host/client - use WCF, .NET Remoting, Named Pipes or direct TCP/IP communication mechanism where one process hosts a service interface implementation and the calling process (the client) uses a proxy of the service interface to serialize and communicate the call to the host process - this is the best approach if you want a stateful request/response interaction
  3. Message queue - use a message queue like MSMQ where one process sends a message to the queue and the other picks it up and executes the method

I'm sure there are others but these are the three most common methods.

My favorite is a light weight service host/client scenario. There are several lightweight utilities to make this easy for you. You can use RemotingLite or my own extension of RemotingLite which supports Named Pipes called DuoVia.Net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.