0

我正在使用 iperf-2.0.5-2-win32 工具来查找网络带宽。我在 c# 中编写了代码,它打开 cmd 提示符,传递 iperf 参数来启动服务器端和客户端。iperf-2.0.5-2-win32 exe不会直接打开,只能通过cmd提示打开。目前输出(传输速率和带宽)显示在 cmd 提示符本身上。我希望这些输出显示在我也尝试过 StreamReader 的文本框中。但是它取null,我也尝试过OutputDataReceived Event,它也取null。找到了一些 ipconfig 和 ping 的代码。但这些代码不适用于 iperf 代码。

button_click event(),
{
Process Client_proc = new Process();
ProcessStartInfo Client_command = new ProcessStartInfo("cmd.exe"); 
string ip = txtIP.Text;
Client_command.CreateNoWindow = true;
Client_command.WindowStyle = ProcessWindowStyle.Hidden;
Client_command.WorkingDirectory = @"E:\Iperf\RunEXE_Through_Application\iperf-2.0.5-2-win32";
Client_command.Arguments = "/c START iperf -c " + ip;
Client_proc.StartInfo = Client_command;
Client_command.RedirectStandardOutput = true;
Client_command.UseShellExecute = false;
Client_proc.OutputDataReceived += new DataReceivedEventHandler(Client_proc_OutputDataReceived);
Client_proc.Start(); 
Client_proc.BeginOutputReadLine(); 
Client_proc.WaitForExit();
}

void Client_proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string newLine = e.Data.Trim() + Environment.NewLine;
MethodInvoker append = () => txtOutput.Text += newLine;
txtOutput.BeginInvoke(append);
}
}

请帮助我。感谢早期的回复谢谢

4

2 回答 2

1

你使用这个完整的代码来处理它并不完美(使用多个流时的一些问题)

public void RunProcess(string FileName, string Arguments, bool EventWhenExit )
{
    process = new Process();

    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.LoadUserProfile = false;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.FileName = FileName; // Gets or sets the application or document to start.
    process.StartInfo.Arguments = Arguments;//Gets or sets the set of command-line arguments to use when starting the application      
    Thread.Sleep(1000);
    if (EventWhenExit)
    {
        process.EnableRaisingEvents = true;

        process.Exited += new EventHandler(myprocess_Exited);/*New line */

    }

    process.Start();
    process.BeginOutputReadLine();
    PID = process.Id;


}

private void myprocess_Exited(object sender, EventArgs e)
{
    process.Refresh();
    Thread.Sleep(2000);
    onProcessEnd(this, "ENDOF " + Proc.ToString());
    Console.WriteLine("Process exsiting ");
}


private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{

    string OutputFromProcess = e.Data;
    //fire event to event handler class for further use
    onDataOutputFromProcess(this, OutputFromProcess, Proc.ToString());
}

比在您的 GUI 层中,您应该绑定到那里的onDataOutputFromProcess事件,您应该有类似的东西

if (screenToPrint.InvokeRequired) //&& this.Visible)
{
    try
    {
        this.Invoke(new Action<AppendToScreenParam>(AppendTextFullConfig), new object[] { append });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    return;
}
else
{
    screenToPrint.SelectionFont = font;
    screenToPrint.SelectionColor = append.Color;
    //screenToPrint.AppendText(append.Message);
    string TextToPrint = string.Format("{0}\n", append.Message);
    screenToPrint.AppendText(TextToPrint);
}

}

于 2014-08-28T09:08:07.200 回答
0

可能是因为 iperf 进程返回错误。订阅ErrorDataReceived事件Client_proc.ErrorDataReceived += Client_proc_ErrorDataReceived;并查看结果。如果命令返回错误,您可以看到错误消息作为输出。

void Client_proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        this.txtOutput.BeginInvoke(new MethodInvoker(() => { this.txtOutput.Text = e.Data; }));
    }
}
于 2014-05-21T14:04:56.637 回答