Cant get process error output using process.ErrorDataReceived C #

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(); //run polling on stored logs to print them to screen PollingService(); } 

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 /* ProcessRunning*/) { 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(); } 
+6
source share
1 answer

I do not see the BeginErrorReadLine() call BeginErrorReadLine() in the code that you submitted. If you do not call this method, the Process class does not actually redirect stderr to your event handler.

I think this is a problem, but if you actually call it somewhere (and just did not show it), then you should think that there are some strange console programs that are not actually used by stderr (or stdout) for output mistakes. Instead, they are written directly to the console window or some other non-standard mechanism. In these cases, you cannot get the error output by redirecting stderr.

You can identify these programs by redirecting their output in a command, for example, using, for example, iperf.exe 2> foo.txt . The descriptor file is stderr 2 , and therefore the syntax redirects this file descriptor to a file called foo.txt . If the file is empty and you see errors on the screen, then the program is one of those strange programs.

But actually, I think you probably just forgot to call BeginErrorReadLine() . :)

+7
source

Source: https://habr.com/ru/post/982164/


All Articles