在WPF应用程序中运行其他控制台进程之前,如何等待控制台进程完成? [英] How to wait for a console process to complete BEFORE running the other console process in wpf applications?

查看:173
本文介绍了在WPF应用程序中运行其他控制台进程之前,如何等待控制台进程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个外部进程要运行(2个控制台exe),这些进程将输出提供给控制台.我正在wpf应用程序上运行这些应用程序.我捕获控制台输出并将其用作输入.

I have 2 external process to run(2 console exe) that provide output to console. I am running these apps on wpf application. I capture console output and use it as input.

我该怎么做 1.运行第一个进程.捕获控制台输出. 2.使用第一个过程输出进行一些工作. 3.在我完成一些工作输出之后,运行第二个过程.

here how i do 1. run first process. capture the console output. 2. do some work using the first process output. 3. run the second process after i have the do some work output.

我遇到的问题是我必须等待另一个进程完成并使用其输出,然后才能运行另一个进程.到我要运行第二个进程时,第一个进程还没有完成.

the problem i am having is i have to wait for the other process to finish and use its output before i can run the other process. By the time i want to run the second process, the first process havent been finished.

public MainWindow()
{
    InitializeComponent();
    ExtProcess1();
    ExtProcess2();  //this is executed before ExtProcess1 OutputDataReceived is completely captured
    Instance = this;
}

public static void ExtProcess1()
{
    Process process = new Process();
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
    process.Exited += new EventHandler(process_Exited);
    process.StartInfo.FileName = "test.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;
    process.Start();
    process.PriorityClass = ProcessPriorityClass.RealTime;
    process.BeginOutputReadLine();
    process.WaitForExit();
}

static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (MainWindow.Instance.lbLog.Dispatcher.Thread == Thread.CurrentThread)
    {
        String s = (String)e.Data;
        MainWindow.Instance.lbLog.Items.Add(s);
    }
    else
    {
        MainWindow.Instance.lbLog.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send,
            new EventHandler<DataReceivedEventArgs>(corePingPid_OutputDataReceived), sender, new object[] { e });
    }
}

在运行第二个进程之前,如何强制wpf应用程序等待第一个进程?

How do i force the wpf application to wait for the first process BEFORE running the second process?

推荐答案

假定您正在使用Process类来执行和捕获控制台的输出,则可以使用

Assuming you are using Process class to execute and capture output for console, you can use Process.WaitForExit method. It will block the current thread till the process finish the execution. There is another overload that can block the current thread till the process finishes or specified timeout period elapsed.

这篇关于在WPF应用程序中运行其他控制台进程之前,如何等待控制台进程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆