通过OutputDataReceived事件捕获过程输出 [英] Capturing process output via OutputDataReceived event
本文介绍了通过OutputDataReceived事件捕获过程输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图捕捉过程输出实时(而它的运行)。我使用的代码是相当简单的(见下文)。出于某种奇怪的原因OutputDataReceived事件永远不会被调用。为什么呢?
私人无效button2_Click(对象发件人,EventArgs五)
{
//安装过程启动信息
变种的ProcessStartInfo =新的ProcessStartInfo(ping.exe的,-t -n 3 192.168.100.1)
{
UseShellExecute =假,
RedirectStandardOutput = TRUE
};
//安装的过程
mProcess =新的Process {StartInfo的=的ProcessStartInfo,EnableRaisingEvents = TRUE};
//注册事件
mProcess.OutputDataReceived + = OnOutputDataReceived;
//启动过程
mProcess.Start();
mProcess.WaitForExit();
}
无效OnOutputDataReceived(对象发件人,DataReceivedEventArgs E)
{
//不会被调用...
}
解决方案
您需要调用
mProcess.BeginOutputReadLine();
的 BeginOutputReadLine - 起始于应用程序的重定向StandardOutput流异步读取操作
I'm trying to capture process output in "realtime" (while it's running). The code I use is rather simple (see below). For some strange reason the OutputDataReceived event is never called. Why?
private void button2_Click(object sender, EventArgs e)
{
// Setup the process start info
var processStartInfo = new ProcessStartInfo("ping.exe", "-t -n 3 192.168.100.1")
{
UseShellExecute = false,
RedirectStandardOutput = true
};
// Setup the process
mProcess = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
// Register event
mProcess.OutputDataReceived += OnOutputDataReceived;
// Start process
mProcess.Start();
mProcess.WaitForExit();
}
void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
//Never gets called...
}
解决方案
You need to call
mProcess.BeginOutputReadLine();
BeginOutputReadLine - "Begins asynchronous read operations on the redirected StandardOutput stream of the application."
这篇关于通过OutputDataReceived事件捕获过程输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文