I am designing GUI for iperf3.exe. When I run iperf3.exe, the text box appears after it finishes the process. I want to read output while doing the process. I try async method and task but all the time process finish then read output. I can read output if I write ping commands, but can't read output of iperf3.exe at real time. How can I read output while iperf3.exe running?
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);
}
}