解决方案是使用多线程,首先添加:
使用 System.Threading;
然后看下面的代码:
Process process = new Process();
process.StartInfo = new ProcessStartInfo("app.exe");
process.StartInfo.WorkingDirectory = "";
process.StartInfo.Arguments = "some arguments";
//启动一个启动进程的新线程,因此当您调用 WaitForExit 时它不会冻结主线程
Thread th= new Thread(() =>
{
process.Start();
process.WaitForExit();
});
th.Start();
如果你想背靠背运行多个进程,这是另一种情况,你需要使用类似进程列表的东西,看看下面的代码
List<Process> processes = new List<Process>();;
Process process = new Process();
process.StartInfo = new ProcessStartInfo("app.exe");
process.StartInfo.WorkingDirectory = "";
process.StartInfo.Arguments = "some arguments";
processes.Add(process);
// 我手动添加另一个,但您可以使用循环为例
Process process2 = new Process();
process.StartInfo = new ProcessStartInfo("app.exe");
process.StartInfo.WorkingDirectory = "";
process.StartInfo.Arguments = "some arguments";
processes.Add(process2);
// 然后你将通过你的线程启动它们,第二个进程将等到第一个完成而不阻塞 UI
Thread th= new Thread(() =>
{
for (int i = 0; i < processes.Count; i++)
{
processes[i].Start();
processes[i].WaitForExit();
}
});
th.Start();