I have created a Form application that I have been using for some time. Now I want to catch the StandardError my process, as well as its standartOutput
I looked at the answers in SO and MSDN and still cannot get it correctly
My code is:
public void RunProcess(string FileName, string Arguments,, bool IsPrintOutput = true) { process = new Process(); process.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent); if (IsPrintOutput) process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent); process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = FileName; process.StartInfo.Arguments = Arguments; if (EventWhenExit) { process.EnableRaisingEvents = true; process.Exited += new EventHandler(myprocess_Exited); } process.Start(); process.BeginOutputReadLine();
I tested it with Iperf , and I see that when I run it with the correct argument, I get the correct output, but when I just send it without arguments, I see that with cmd I get
C:\>iperf.exe Usage: iperf [-s|-c host] [options] Try `iperf --help' for more information.
And my application does not get anything!
What am I missing here? thanks
You can stop reading here! If you want the details of the internal method to continue below:
private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e) { string ProcessOutput = e.Data; ProcessLog.Add(e.Data); } private void PollingService() { var T = new Thread (()=> { while (true ) { if (ProcessLogIndex < ProcessLog.Count) { lock (this) { var tempList = ProcessLog.GetRange(ProcessLogIndex, ProcessLog.Count - ProcessLogIndex); ProcessLogIndex = ProcessLog.Count; foreach (var ToSend in tempList) { onDataOutputFromProcess(this, ToSend, sProcessNameID.ToString()); } } } Thread.Sleep(400); } }); T.IsBackground = true; T.Start(); }
source share