gpt4 book ai didi

c# - 通过 OutputDataReceived 事件捕获进程输出

转载 作者:IT王子 更新时间:2023-10-29 03:59:54 24 4
gpt4 key购买 nike

我正在尝试“实时”(在运行时)捕获流程输出。我使用的代码相当简单(见下文)。由于某些奇怪的原因,永远不会调用 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 - “在应用程序的重定向标准输出流上开始异步读取操作。”

关于c# - 通过 OutputDataReceived 事件捕获进程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11631443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com