Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Diagnostics.Process, asyncronous reading StandardOutput and StandardError streams

System.Diagnostics.Process, asyncronous reading StandardOutput and StandardError streams

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    Hi All, I'm using System.Diagnostics.Process for running a background task, from which i need to read the standard out and standard error. I'm doing this asyncronously with the Process.ErrorDataReceived, Process.OutputDataReceived events, and Process.BeginOutputReadLine() and Process.BeginErrorReadLine(). I also need to know when the process has exited, and i've received all the data. The problem is, that the Process.Exited event is fired before all of the data has been processed. I need to tidy up other stuff when the process has exited and all the data from stdout/stderr has been received. But I cannot for the life of me find a way of working out when that is the case. Can anyone help? thanks Jon

    using System.Beer;

    H 1 Reply Last reply
    0
    • J Jon Hulatt

      Hi All, I'm using System.Diagnostics.Process for running a background task, from which i need to read the standard out and standard error. I'm doing this asyncronously with the Process.ErrorDataReceived, Process.OutputDataReceived events, and Process.BeginOutputReadLine() and Process.BeginErrorReadLine(). I also need to know when the process has exited, and i've received all the data. The problem is, that the Process.Exited event is fired before all of the data has been processed. I need to tidy up other stuff when the process has exited and all the data from stdout/stderr has been received. But I cannot for the life of me find a way of working out when that is the case. Can anyone help? thanks Jon

      using System.Beer;

      H Offline
      H Offline
      Heath Higgins
      wrote on last edited by
      #2

      Hi Jon, I've been struggling with this one for a while myself and was able to create a scenario where standard out/err data were not handled by dumping a large number of lines from the process right before exiting. I had hoped to be able to switch from asynchronous to synchronous reads once the WaitForExit() returns, and then receive the remaining data with a process.StandardOutput.ReadToEnd(). Unfortunately, this is not possible: "You cannot mix asynchronous and synchronous read operations on the redirected StandardOutput stream. Once the redirected stream of a Process is opened in either asynchronous or synchronous mode, all further read operations on that stream must be in the same mode. If you cancel an asynchronous read operation on StandardOutput and then need to read from the stream again, you must use BeginOutputReadLine to resume asynchronous read operations. Do not follow CancelOutputRead with a call to the synchronous read methods of StandardOutput such as Read, ReadLine, or ReadToEnd." http://msdn.microsoft.com/en-us/library/system.diagnostics.process.canceloutputread.aspx[^] As far as I can tell, this applies to anything you could check (process.StandardOutput.EndOfStream) to see if it were necessary for you to delay cleanup. Unless I'm missing something, which is entirely possible, this seems like a shortcoming of the Process class, because it essentially means that you can't trust the asynchronous interface to provide you with all of your data. Sadly, this seems to leave us with synchronous IO, perhaps a couple extra threads, and the additional synchronization issues that this approach entails (see here[^] for a deadlock condition to watch out for). The only other thing that has worked for me so far, and this is sort of hackish, is to sleep the thread that handles the Exited event (or a return from WaitForExit) to give the DataReceived events time to fire a few more times. I ended up with something like this: RunningProcess.Start(); RunningProcess.BeginOutputReadLine();

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups