Bor*_*ris 40 c# stdout process
我正试图以"实时"(当它正在运行时)捕获过程输出.我使用的代码相当简单(见下文).由于某些奇怪的原因,从不调用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...
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*ris 74
你需要打电话
mProcess.BeginOutputReadLine();
Run Code Online (Sandbox Code Playgroud)
BeginOutputReadLine - "在应用程序的重定向StandardOutput流上开始异步读取操作."