我正在尝试“实时”捕获进程输出(运行时)。我使用的代码非常简单(请参见下文)。由于某些奇怪的原因,从不调用OutputDataReceived事件。为什么?
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...
}
最佳答案
你需要打电话
mProcess.BeginOutputReadLine();
BeginOutputReadLine-“开始对应用程序的重定向StandardOutput流进行异步读取操作。”
关于c# - 通过OutputDataReceived事件捕获过程输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11631443/