我正在为 iperf3.exe 设计 GUI。当我运行 iperf3.exe 时,文本框会在它完成进程后出现。我想在执行进程时读取输出。我尝试异步方法和任务,但进程总是在完成后才读取输出。如果我编写 ping 命令,我可以读取输出,但无法实时读取 iperf3.exe 的输出。如何在 iperf3.exe 运行时读取输出?
private void CMDyazma(string commandline_server)
{
try
{ string output = "";
bool boolean = false;
string eOut = null;
Process pr = new Process();
pr.StartInfo.UseShellExecute = false;
pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.RedirectStandardError = true;
pr.StartInfo.CreateNoWindow = true;
pr.StartInfo.FileName = "D:\\iperf3\\iperf3.exe";
pr.StartInfo.Arguments = commandline_server;
pr.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
pr.ErrorDataReceived += new DataReceivedEventHandler(ErrorHandler);
pr.Start();
//pr.OutputDataReceived += new DataReceivedEventHandler(pr_OutputDataReceived);
// pr.OutputDataReceived += (s, e) => { e.Data ; };
pr.BeginOutputReadLine();
//string output1 = pr.StandardOutput.ReadToEnd();
pr.BeginErrorReadLine();
pr.WaitForExit(1000);
}
catch
{
//return "Error: " + e.Message;
}
}
private static void OutputHandler(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine("Output: " + e.Data);
}
}
private static void ErrorHandler(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine("Error: " + e.Data);
}
}