我有两个外部进程需要运行(2个控制台exe),它们将输出到控制台。我在wpf应用程序上运行这些应用程序。我捕获控制台输出并将其用作输入。
这是我的步骤: 1. 运行第一个进程。捕获控制台输出。 2. 使用第一个进程的输出进行一些工作。 3. 在我有了一些工作输出后运行第二个进程。
问题是,在运行另一个进程之前,我必须等待其他进程完成并使用其输出。当我想要运行第二个进程时,第一个进程还没有完成。
如何强制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应用在运行第二个进程之前等待第一个进程完成?