1

I'd like to launch a command line app multiple times and be told when each instance is finished (eg: exits). I've tried starting a process:

ProcessStartInfo startInfo = new ProcessStartInfo("c:mycmdline.exe");
proc = Process.Start(startInfo);
proc.WaitForExit();

But as can be imagined the process waits until it exits (as per the last line). Is there a way to get a non-blocking callback after each instance exits?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Stu
  • 77
  • 5

1 Answers1

0

Here's an example of starting 3 processes as tasks and waiting for completion.

Task[] tasks = new Task[3];
        for (int i = 0; i < 3; i++)
        {

            int x = i;
            tasks[i] = Task.Factory.StartNew(() =>
            {

                // the processPath will be different based on whatever the value for i is
                ProcessStartInfo startInfo = null;
                switch (x)
                {
                    case 0:
                        startInfo = new ProcessStartInfo("c:\\windows\\notepad.exe");
                        break;
                    case 1:
                        startInfo = new ProcessStartInfo("c:\\windows\\explorer.exe");
                        break;
                    case 2:
                        startInfo = new ProcessStartInfo("c:\\windows\\regedit.exe");
                        break;
                }
                Process proc = Process.Start(startInfo);
                proc.WaitForExit();
            });
        }
        Task.WaitAll(tasks);
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40