为什么我可以在屏幕上输出输出 [英] Why can I get the output to print on screen

查看:81
本文介绍了为什么我可以在屏幕上输出输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法获得输出。我想这是因为我没有在最好的地方获得输出。但如果这是真的,为什么控制台写信线根本不工作





 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Diagnostics;



命名空间 AutomatedIperfTester
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void startButton_Click( object sender,EventArgs e)
{
string ipAddress = IPAddress_textBox.Text;
string MaxVolume = MaxVolume_textBox.Text;
string Duration = Duration_textBox.Text;
string UDPTCP = UDPTCP_comboBox.Text;

处理runServer = new Process();
ProcessStartInfo CMD_commandServer = new ProcessStartInfo( CMD .EXE);
CMD_commandServer.CreateNoWindow = true ;
CMD_commandServer.WindowStyle = ProcessWindowStyle.Hidden;
CMD_commandServer.WorkingDirectory = @ C:\Users\userName \Downloads\iperf< /跨度>;
CMD_commandServer.Arguments = / c START iperf -s -D;
CMD_commandServer.UseShellExecute = false ;
CMD_commandServer.RedirectStandardOutput = true ;

runServer.StartInfo = CMD_commandServer;
runServer.Start();
runServer.WaitForExit();

// 需要在这里添加一个等待命令,直到第一个进程成功执行为止

处理runClient = new Process();
ProcessStartInfo CMD_commandClient = new ProcessStartInfo( CMD .EXE);
CMD_commandClient.CreateNoWindow = true ;
CMD_commandClient.WindowStyle = ProcessWindowStyle.Hidden;
CMD_commandClient.WorkingDirectory = @ C:\Users\userName \Downloads\iperf< /跨度>;
CMD_commandClient.Arguments = String .Format( / c START iperf -c {0},ipAddress);
CMD_commandClient.UseShellExecute = false ;
CMD_commandClient.RedirectStandardOutput = true ;

runClient.StartInfo = CMD_commandClient;
runClient.Start();
runClient.WaitForExit();

string output = runClient.StandardOutput.ReadToEnd();
// 由于某种原因无法正常工作
Console.WriteLine(输出);

}

private void IPAddress_textBox_TextChanged(< span class =code-keyword> object
sender,EventArgs e)
{
// 为什么不需要这个?
// string ipAddress = IPAddress_textBox.Text ;
}
}
}

解决方案

对于初学者,我认为您已检查VS的输出窗格并使用Console.WriteLine作为调试语句?请尝试使用Debug.WriteLine,因为它会在生产版本中自动删除。



然后使用调试器。在WriteLine语句上放置一个断点,并使用调试器查看输出的确切内容。有什么吗?如果没有,这就解释了为什么你没有看到任何输出。因此,首先从流程中删除无窗口和重定向,然后查看屏幕上发生的情况。那里有输出吗?如果没有,那就是你的问题。因此,通过命令提示符手动运行该命令,看看会发生什么。等等......



我们不能为你做任何事 - 我们没有你的exe并且不能运行它来检查发生了什么对于你。


让我们来看看你的命令行并弄清楚它在做什么



 cmd / c启动iperf参数





1)cmd - 运行命令处理器cmd.exe

2)启动iperf - 运行另一个命令处理器并要求它运行iperf



您重定向的Process实例是第一个命令处理器。



如果你已经完成了

 cmd / c iperf参数



然后重定向可能会起作用,因为iperf将共享为cmd.exe创建的控制台窗口。认为cmd.exe启动控制台但它没有启动是一种常见的误解,cmd.exe只是一个在控制台窗口中运行的用户界面程序。通过Process类启动单独的进程时,通常不需要cmd.exe,通常只会妨碍。



用于的最简单和正确的命令行Process类将是

 iperf参数



所以使用这样的东西

 ProcessStartInfo psi =  new  ProcessStartInfo(); 
psi.FileName = iperf.exe的完整路径
psi.Arguments = iperf参数
psi.CreateNoWindow = true ;
psi.UseShellExecute = false ;
流程p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
someTextBox.Text = output;



请注意,在等待退出之前通常会读取输出。



艾伦。


For some reason i cant get the output. i guess it's because i didnt get the output in the forst place. but if this is true why the console writeline doesnt work at all


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;



namespace AutomatedIperfTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            string ipAddress = IPAddress_textBox.Text;
            string MaxVolume = MaxVolume_textBox.Text;
            string Duration  = Duration_textBox.Text;
            string UDPTCP = UDPTCP_comboBox.Text;

            Process runServer = new Process();
            ProcessStartInfo CMD_commandServer = new ProcessStartInfo("CMD.exe");
            CMD_commandServer.CreateNoWindow = true;
            CMD_commandServer.WindowStyle = ProcessWindowStyle.Hidden;
            CMD_commandServer.WorkingDirectory = @"C:\Users\userName\Downloads\iperf";
            CMD_commandServer.Arguments = "/c START iperf -s -D";
            CMD_commandServer.UseShellExecute = false;
            CMD_commandServer.RedirectStandardOutput = true;
            
            runServer.StartInfo = CMD_commandServer;
            runServer.Start();
            runServer.WaitForExit();

            // need to add here a wait command till the first process is successfully executed

            Process runClient = new Process();
            ProcessStartInfo CMD_commandClient = new ProcessStartInfo("CMD.exe");
            CMD_commandClient.CreateNoWindow = true;
            CMD_commandClient.WindowStyle = ProcessWindowStyle.Hidden;
            CMD_commandClient.WorkingDirectory = @"C:\Users\userName\Downloads\iperf";
            CMD_commandClient.Arguments = String.Format("/c START iperf -c {0}", ipAddress);
            CMD_commandClient.UseShellExecute = false;
            CMD_commandClient.RedirectStandardOutput = true;

            runClient.StartInfo = CMD_commandClient;
            runClient.Start();
            runClient.WaitForExit();

            string output = runClient.StandardOutput.ReadToEnd();
            //doesnt work for some reason
            Console.WriteLine(output);
            
        }

        private void IPAddress_textBox_TextChanged(object sender, EventArgs e)
        {
            //why is this not needed?
            //string ipAddress = IPAddress_textBox.Text;
        }
    }
}

解决方案

For starters, I assume that you have checked the Output pane of VS and are using Console.WriteLine as a debugging statement? Try using Debug.WriteLine instead, as it is automatically removed in production builds.

Then use the debugger. put a breakpoint on the WriteLine statement, and look at the exact content of "output" using the debugger. Anything in it? If not, that explains why you don't see any output. So start by removing the "no window" and "redirect" from your process and see what happens on screen. Is there any output there? If not, that's you problem. So run the command manually via a command prompt and see what happens. And so forth...

We can't do any of this for you - we don't have your exe and can't run it to check what happens for you.


Let's take a look at your command line and figure out what it's doing

cmd /c start iperf arguments



1) cmd - run the command processor cmd.exe
2) start iperf - run another command processor and ask it to run iperf

The Process instance you have redirected is the first command processor.

If you had done

cmd /c iperf arguments


then the redirection would probably work as iperf would be sharing the console window created for cmd.exe. It is a common misconception to think that cmd.exe starts a console but it does not, and cmd.exe is simply a user interface program that runs in a console window. When starting a separate process via the Process class, cmd.exe is usually not needed and often just gets in the way.

The simplest and correct command line for use with the Process class would be

iperf arguments


So use something like this

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Full path to iperf.exe
psi.Arguments = iperf arguments
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
someTextBox.Text = output;


Note that it's usual to read the output before waiting for exit.

Alan.


这篇关于为什么我可以在屏幕上输出输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆