6

I want my program to wait for all processes to finish and then delete some temporary files. I already have a block of code that can do this when dealing with one process at a time, but I can't make it work for multiple processes.

I'll start by showing the code that works. This code correctly runs a process, waits for it to finish, and then deletes the temporary file:

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

The above is neatly contained within the foreach loop. What I can't get working is a parallel version of this where the wait statement is outside of the foreach loop:

  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

I think I just need to initialize myWait before going into the foreach loop, but I don't know how to do that. How do I initialize a process without assigning it to anything? Is there some other simple way to do this?

3

3 Answers 3

10

Not sure if this approach was in any of the linked questions from the comments, but here's one that makes use of List<Task> and 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
  • WaitForExitAsync() is available starting in CLR 5+ but, if you don't have that available, wrapping the WaitForExit() in an anonymous delegate is a much smaller change than upgrading your runtime. Thanks! Apr 20, 2022 at 8:14
3

I would store the process handles in a dictionary of script vs process handle, and then later wait for all processes to exit. Below is the code:

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);
}
1
  • Iterating a TKey over a Dictionary<TKey, TValue> and having the first line as an assigment for the TValue is the same as iterating a Pair<TKey, TValue> over the dictionary and using the Key and Value properties, except you avoid using the hashing algorithm.
    – Mephy
    Dec 29, 2014 at 2:23
3

Alternative way for future viewers:

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

or using 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.