【问题标题】:How to display output of iperf cmd prompt in textbox如何在文本框中显示 iperf cmd 提示符的输出
【发布时间】:2014-05-21 11:53:07
【问题描述】:

我正在使用 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);
}
}

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

【问题讨论】:

  • “它需要 null”到底是什么意思?
  • @hSchroedl- 如果我保留断点并且 E.Data() 的检查值为 null" 如果我​​保留断点并且在 Client_proc_OutputDataReceived 中检查 e.Data() 的值为 null
  • 嗯。我自己尝试了 iperf,但即使我从 cmd 窗口启动它也无法获得输出......所以调试这对我来说有点困难。我建议您像往常一样从命令行执行它,然后查看输出结果。我注意到的另一个可能的问题是“START”将打开另一个窗口/进程,您不会附加任何侦听器/流读取器。这就是我所拥有的,对不起。

标签: c# command-prompt iperf


【解决方案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);
}

}

【讨论】:

    【解决方案2】:

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

    void Client_proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
            this.txtOutput.BeginInvoke(new MethodInvoker(() => { this.txtOutput.Text = e.Data; }));
        }
    }
    

    【讨论】:

    • @CuriousPen-No CuriousPen,我在同一个命令提示符下得到输出,我没有收到任何错误消息。我希望将命令提示符输出重定向到文本框。我被困在这部分。你能看看这个
    • @user1244453 奇怪的是我可以从 ErrorDataReceivent 事件中获取所有命令行输出。例如,您可以尝试使用 iperf 命令使用 -v 或 --help 参数。所有输出都写入ErrorStream
    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2017-03-01
    • 2019-02-07
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多