从应用程序中并行运行两个批处理文件

时间:2015-08-10 06:42:39

标签: c# process system.diagnostics

我想并行执行两个批处理文件。我在下面发布了我使用的代码。但每当我运行此脚本时,它只执行一个批处理文件。

但每当我指定文件cmd.exe时,它都会运行两个进程。

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();

1 个答案:

答案 0 :(得分:1)

只需使用WaitForExit()和UseShellExecute = false:

运行proc
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;

此致