0

I want to execute two batch files in parallel. I have posted below the code which I use. But whenever I run this script, it executes only one batch file.

But whenever I specify file cmd.exe, it runs both processes.

Process spyAllEth0 = new Process();
Process spyAllWlan0= new Process();

spyAllEth0.StartInfo.FileName = "D:/work/Platform/mcg/TCPDumpForMcg/SpyEth0MCG1.bat";
spyAllEth0.EnableRaisingEvents = true;
spyAllEth0.StartInfo.CreateNoWindow = false;
spyAllEth0.Start();

spyAllWlan0.StartInfo.FileName = "D:/work/Platform/mcg/TCPDumpForMcg/SpyWlan0MCG1.bat";
spyAllWlan0.EnableRaisingEvents = true;
spyAllWlan0.StartInfo.CreateNoWindow = false;
spyAllWlan0.Start();
TaW
  • 48,779
  • 8
  • 56
  • 89
Hiren
  • 57
  • 1
  • 10
  • I believe what you're looking for can be found here: http://stackoverflow.com/questions/31391473/what-is-the-fastest-way-to-run-an-exe-command-many-times-in-c – Michiel Bugher Mar 23 '16 at 23:12

1 Answers1

1

Just run both proc with WaitForExit() and UseShellExecute = false:

var proc = new Process();

proc.StartInfo.WorkingDirectory = path;
proc.StartInfo.FileName = "my_exe.exe";
proc.StartInfo.Arguments = "my_args";
proc.StartInfo.UseShellExecute = false;

var proc2 = new Process();

proc2.StartInfo.WorkingDirectory = path;
proc2.StartInfo.FileName = "my_exe2.exe";
proc2.StartInfo.Arguments = "my_args";
proc2.StartInfo.UseShellExecute = false;

proc.Start();
proc2.Start();

proc.WaitForExit();
proc2.WaitForExit();

if (proc.ExitCode != 0)
{
    return proc.ExitCode;
}

if (proc2.ExitCode != 0)
{
    return proc2.ExitCode;
}

return 0;

Regards,

lostcitizen
  • 420
  • 4
  • 14
  • Thanks for your response.. I want to run both batch files in parallel instead of one by one. I found one solution for that, create a parent batch file which calls two required batch files. And call parent file from c#. :) May be you can see [Run multiple batch files](http://stackoverflow.com/questions/1103994/how-to-run-multiple-bat-files-within-a-bat-file) – Hiren Aug 10 '15 at 08:57
  • This actually will run in parallel. It won't wait untill both process have started, and it will wait untill they have finished. Of course you can do it with batch scripting, but you asked for a c# .net answer :P Regards – lostcitizen Aug 10 '15 at 09:29