views:

228

answers:

4

I have a child.exe which takes command line arguments. I need to start that child.exe from another parent.exe application and need to pass different command line arguments to that child.exe. I tried with the following code.

Process process = new Process();
        process.StartInfo.FileName = @"R:\bin\child.exe";
        process.StartInfo.Arguments = "CONSUMER";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

process = new Process();
        process.StartInfo.FileName = @"R:\bin\child.exe";
        process.StartInfo.Arguments = "SUPERVISOR";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

But the problem here is each time when I call process.Start(), a separate exe is created. I need only one instance of child.exe running which would accept different command line arguments. Any help is appreciated.

+4  A: 

Of course it's going to create a new process, if you want to pass an existing process new arguments you're best of with some kind of IPC.

Lloyd
A: 

in code create a bat file whish will contain your parameters. the parent exe will call the bat file. after the parent end delete the bat file.

Mladen Prajdic
A: 

Firstly, your child application could be set up to use a Mutex to ensure it is only run once.

Secondly, you probably want to look into the Remoting functionality to allow you to communicate across your different processes to achieve the effect you are looking for.

Jon Grant
A: 

Or dynamically load the assembly into the parent.exe process and call a method in it. You can even do it in an isolated AppDomain, which (if child.exe is written in managed code) will likely be the solution you really want. Take a look at this MSDN article for starters:

http://msdn.microsoft.com/en-us/library/6s0z09xw.aspx

280Z28