[英]How to wait for a console process to complete BEFORE running the other console process in wpf applications?
我有2个外部进程运行(2个控制台exe),为控制台提供输出。 我在wpf应用程序上运行这些应用程序。 我捕获控制台输出并将其用作输入。
这里我怎么做1.运行第一个过程。 捕获控制台输出。 2.使用第一个流程输出做一些工作。 3.在我做了一些工作输出后运行第二个过程。
我遇到的问题是我必须等待其他进程完成并使用其输出才能运行其他进程。 当我想运行第二个过程时,第一个过程还没有完成。
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应用程序等待第一个进程?
假设您正在使用Process类来执行和捕获控制台的输出,您可以使用Process.WaitForExit方法。 它将阻塞当前线程,直到进程完成执行。 还有另一个重载可以阻塞当前线程,直到进程结束或指定的超时时间结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.