0

I have a problem here.....

I have a List of ProcessStartInfo as List<ProcessStartInfo>

I want to Start processes in Series i.e

if i starts first process then second process in the List should start only when the first process (Exits or Aborts)...

I tries it like .....

    private void startSeriesExecution()
    {
        List<string> programpath = new List<string>();
        programpath.Add(@"C:\Program Files\Internet Explorer\iexplore.exe");
        programpath.Add(@"C:\Program Files\Microsoft Office\Office12\WINWORD.EXE");

        foreach (var VARIABLE in programpath)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo
                                             {
                                                 FileName = VARIABLE,
                                                 Arguments =
                                                     "www.google.com"
                                             };

            try
            {
                using (Process process=Process.Start(startInfo))
                {
                    if (process.WaitForExit(Int32.MaxValue))
                    {
                        continue;
                    }
                }
            }
            catch (Exception)
            {
                continue;
            }
        }
    }

I am running startSeriesExecution using a Custom Class for MultiThreading all the process execute at the same time....

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // This function starts the Execution in different thread
        Start.Work(startSeriesExecution).OnComplete(onSeriesExecutionComplete).Run();
    }

Also one more thing i observed...

if 1 Process is already running... lets say IE is running already and now i executed startSeriesExecution() and in this start an already running process (IE)... then the whole for loop is executed... i.e all the process in the list are executed...

Have have now no idea ho to proceed....

Ankesh
  • 4,847
  • 4
  • 38
  • 76

1 Answers1

0

That code looks like it would work. The WaitForExit will pause that thread until it completes.

Also if these processes output a lot of information then you will need to add

process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);

Which is a good idea regardless, otherwise the process could hang until the output and error fields are cleared, which might never happen.

Adding the events to the process will keep them outputting and hence not clog it up so to speak.

UPDATE:

If you want to check if a process is already running you can do this by this: stackoverflow.com/a/51149/540339

Then you would have to loop and wait using Thread.Sleep(xxxx) until that process closes.

Community
  • 1
  • 1
Adam
  • 16,089
  • 6
  • 66
  • 109
  • What if i am executing the function `startSeriesExecution()` through multithreading... ie in a DifferentThread Using Dispatcher.. – Ankesh Dec 05 '11 at 06:13
  • There would be no need to do this through multithreading as each process will be started in its own thread anyway. So I am not sure what you are trying to accomplish. – Adam Dec 05 '11 at 06:34
  • If you must use multithreading for some reason you will have to create a loop to check if the process is already running. Such as in this answer: http://stackoverflow.com/a/51149/540339 – Adam Dec 05 '11 at 06:35