svbnet
svbnet

Reputation: 1090

Launch processes one after another in C#

Is there a way to iterate through an array/list and launch a process, and wait until it's exited until launching the next, and so on?

I have the code:

string[] processPaths = new string[]{ @"c:\foo.exe", @"c:\bar.exe" };
foreach(string s in processPaths){
    Process p = new Process();
    p.Exited += (obj, ev) => { continue; };
}

but obviously processes are executed asynchronously and the lambda function isn't part of the foreach loop. It's a console application, and I don't mind if it runs on the main thread.

Upvotes: 1

Views: 1094

Answers (2)

Sergii Bidnyi
Sergii Bidnyi

Reputation: 306

foreach (var name in names)
{
   var process = Process.Start(name);
   process.WaitForExit();
}

Upvotes: 3

Gonzalo
Gonzalo

Reputation: 21175

Process.WaitForExit() is what you are looking for.

Instructs the Process component to wait indefinitely for the associated process to exit.

Upvotes: 4

Related Questions