Run multiple Process.Start () processes sequentially in C #

I am executing 3 exes using Process.Start()C # in my application. I want to run all these exes sequentially. Right now, each Process.Start()is running independently in parallel.

eg:

Process.Start(exe1ForCopying_A_50_Mb_File);      
Process.Start(exe2ForCopying_A_10_Mb_File);  
Process.Start(exe3ForCopying_A_20_Mb_File);

I want my second one to Process.Start()start executing ONLY AFTER AFTER the first one to Process.Start()finish copying a 50 MB file (which would take about 1 or 2 minutes).

Any suggestions?

Thank.

+3
source share
3 answers

I think I got the answer myself ..! :)

Process process = new Process(); 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = MyExe; 
startInfo.Arguments = ArgumentsForMyExe; 
process.StartInfo = startInfo; 
process.Start(); 
process.WaitForExit(); // This is the line which answers my question :) 

Thanks for the suggestion VAShhh ..

+8
source

( WaitForExit), .

Process Exited, . Process, Exited, Start; , Process.Start, , Process.Start , , , .

Proof of concept: (does not handle Dispose, access to the queue is not thread safe, although it should be sufficient if it is really serial and so on)

Queue<Process> ProcessesToRun = new Queue<Process>(new []{ new Process("1"), new Process("2"), new Process("3") });

void ProcessExited(object sender, System.EventArgs e) {
    GrabNextProcessAndRun();
}

void GrabNextProcessAndRun() {
    if (ProcessesToRun.Count > 0) {
        Process process = ProcessesToRun.Dequeue();
        process.Exited += ProcessExited;
        process.Start();
    }
}

void TheEntryPoint() {
    GrabNextProcessAndRun();
}
0
source

All Articles