Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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;
        }
    }
}
Posted
Updated 24-Nov-15 22:01pm
v2

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.
 
Share this answer
 
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
C#
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.
 
Share this answer
 
Comments
MrXccv 24-Nov-15 9:26am    
thanks for your help. I have to run the iperf process twice because it's a server client networking test tool. and the program(iperf) does not work with just arguments.. you have to open a CMD and then execute it.
Alan N 24-Nov-15 10:28am    
CMD.exe is the program that manages the prompt in a console window and allows a user to enter instructions such as dir to view the contents of a directory. It does not create a console window, the operating system does that when a console based program is started. CMD.exe is one such program and it is highly unlikely that iperf must be started by CMD.exe. You can try both "CMD /c iperf args" and "iperf args" in your code and decide for yourself.

I think the primary reason for the redirection failure in your code was the use of the start command which meant that the CMD.exe process referenced by the Process object was not the CMD.exe that was hosting iperf.
MrXccv 25-Nov-15 3:54am    
I think your input regarding the CMD is correct but if I remove the "new ProcessStartInfo("CMD");" the code just crashes (win32exception was unhandled -- The system cannot find the file specified ) or freezes
MrXccv 25-Nov-15 3:59am    
Process runServer = new Process();
ProcessStartInfo CMD_commandServer = new ProcessStartInfo();
CMD_commandServer.CreateNoWindow = true;
CMD_commandServer.WindowStyle = ProcessWindowStyle.Hidden;
CMD_commandServer.FileName = @"C:\Users\nirf\Downloads\iperf\iperf.exe";
CMD_commandServer.Arguments = "/c START iperf -s -D";
CMD_commandServer.UseShellExecute = false;
CMD_commandServer.RedirectStandardOutput = true;

This makes the program to freeze
Alan N 25-Nov-15 5:28am    
"/c Start iperf" were arguments to CMD.exe as I explained in my original answer. If the filename is "C:\Users\nirf\Downloads\iperf\iperf.exe" then the only arguments needed are those required by iperf, i.e. "-s -D". Try that and it should be good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900