串行和并行启动过程

发布于 2024-12-19 15:43:42 字数 1836 浏览 3 评论 0原文

我这里有一个问题......

我有一个 ProcessStartInfo 列表,如 List

我想在 Series 中启动进程,即

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

我尝试这样做......

    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 循环...即列表中的所有进程都被执行...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-12-26 15:43:42

该代码看起来可以工作。 WaitForExit 将暂停该线程直到完成。

此外,如果这些进程输出大量信息,那么您将需要添加

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

“Which”无论如何都是一个好主意,否则该进程可能会挂起,直到清除输出和错误字段,而这可能永远不会发生。

将事件添加到流程中将使它们保持输出,因此可以说不会阻塞它。

更新:

如果您想检查进程是否已在运行,可以通过以下方式执行此操作: stackoverflow.com/a/51149/540339< /a>

然后,您必须使用 Thread.Sleep(xxxx) 循环并等待,直到该进程关闭。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文