Question

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....

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top