0

我这里有问题......

我有一个 ProcessStartInfo 列表作为List<ProcessStartInfo>

我想在Seriesie中启动进程

如果我启动第一个进程,那么列表中的第二个进程应该仅在第一个进程(退出或中止)时启动...

我尝试它喜欢......

    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;
            }
        }
    }

我正在startSeriesExecution使用自定义类进行多线程处理,所有进程同时执行....

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

还有一件事我观察到...

如果 1 进程已经在运行...可以说 IE 已经在运行,现在我执行startSeriesExecution()并在此启动一个已经运行的进程(IE)...然后执行整个 for 循环...即列表中的所有进程被执行...

现在不知道如何继续....

4

1 回答 1

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.

于 2011-12-05T05:55:23.523 回答