等待多个进程完成

时间:2014-12-28 21:56:05

标签: c# process parallel-processing wait

我希望我的程序等待所有进程完成,然后删除一些临时文件。我已经有一段代码可以在一次处理一个进程时执行此操作,但我无法使其适用于多个进程。

我将首先展示有效的代码。此代码正确运行进程,等待它完成,然后删除临时文件:

foreach (string script in scriptList)
{
     ProcessStartInfo myProcess = new ProcessStartInfo();
     myProcess.FileName = accoreconsolePath;
     myProcess.Arguments = "/s \"" + script + "\"";
     myProcess.CreateNoWindow = true;         
     Process myWait = Process.Start(myProcess);
     myWait.WaitForExit();

     File.Delete(script);            
}  

以上内容整齐地包含在foreach循环中。我无法工作的是 parallel 版本,其中wait语句在foreach循环之外:

  Process myWait;
  foreach (string script in scriptList)
  {
     ProcessStartInfo myProcess = new ProcessStartInfo();
     myProcess.FileName = accoreconsolePath;
     myProcess.Arguments = "/s \"" + script + "\"";
     myProcess.CreateNoWindow = true;
     myWait = Process.Start(myProcess);
  }
  myWait.WaitForExit();                             //error: use of unassigned local variable "myWait"
  //delete file would go here

我想我只需要在进入myWait循环之前初始化foreach,但我不知道该怎么做。如何在不将其分配给任何内容的情况下初始化流程?还有其他一些简单的方法吗?

3 个答案:

答案 0 :(得分:6)

不确定此方法是否出现在评论中的任何关联问题中,但是这里使用的是List<Task>Task.WaitAll()

List<Task> tasks = new List<Task>();            
foreach (string script in scriptList)
{
    string tmpScript = script;
    tasks.Add(Task.Run(delegate {
        ProcessStartInfo myProcess = new ProcessStartInfo();
        myProcess.FileName = accoreconsolePath;
        myProcess.Arguments = "/s \"" + tmpScript + "\"";
        myProcess.CreateNoWindow = true;
        Process.Start(myProcess).WaitForExit();
        File.Delete(tmpScript);
    }));
}
Task.WaitAll(tasks.ToArray());

答案 1 :(得分:2)

我会将进程句柄存储在脚本与进程句柄的字典中,然后等待所有进程退出。以下是代码:

var processes = new Dictionary<string,Process>();

foreach (string script in scriptList)
{
    ProcessStartInfo myProcess = new ProcessStartInfo();
     myProcess.FileName = accoreconsolePath;
     myProcess.Arguments = "/s \"" + script + "\"";
     myProcess.CreateNoWindow = true;
     myWait = Process.Start(myProcess);
     processes.Add(script, myWait);
}

foreach(var script in processes.Keys)
{
    Process process = processes[script];

    process.WaitForExit();
    process.Close();

    File.Delete(script);
}

答案 2 :(得分:1)

未来观众的替代方式:

var NoOfCores = 4;
Parallel.For(0, scriptList.Count,new ParallelOptions {MaxDegreeOfParallelism=NoOfCores }, index =>
{   
    Process proc= new Process();
    proc.StartInfo.FileName = "filename";
    proc.StartInfo.WorkingDirectory = "optional";
    proc.StartInfo.Arguments = scriptList[index];   
    proc.Start();
    proc.WaitForExit();
    //code here will be executed after finishing each thread
}
//code here will be executed after finishing all the threads

或使用Parallel.ForEach

var NoOfCores = 4;
Parallel.For(0, scriptList,new ParallelOptions {MaxDegreeOfParallelism=NoOfCores }, script=>
{   
    Process proc= new Process();
    proc.StartInfo.FileName = "filename";
    proc.StartInfo.WorkingDirectory = "optional";
    proc.StartInfo.Arguments = script;   
    proc.Start();
    proc.WaitForExit();
    //code here will be executed after finishing each thread
}
//code here will be executed after finishing all the threads