process - Reading console stream continually in c# -
i want read continues output stream of cmd in c#. know can redirect standard output stream , read it. following code:
system.diagnostics.processstartinfo pi= new system.diagnostics.processstartinfo(programpath,params); pi.redirectstandardoutput = true; pi.useshellexecute = false; pi.createnowindow = true; system.diagnostics.process proc= new system.diagnostics.process(); proc.startinfo = pi; proc.start(); string result = proc.standardoutput.readtoend();
but gives whole output @ once. if issue ping
command -t
argument? how can read stream continually?
you're calling readtoend
block until process terminates. can repeatedly call read
or readline
instead. however, should consider either doing in different thread or using events such outputdatareceived
this. (if you're using events, need call beginoutputreadline
enable events - see msdn examples details.)
the reason potentially reading on different thread if need read both standard error , standard output, need make sure process doesn't fill buffer - otherwise block when writing, leading deadlock. using asynchronous approach makes simpler handle this.
Comments
Post a Comment