redirecting both standard output and standard error
-
2012年4月10日 22:14
Hi,
How to catch both standard output and standard error redirections?
From
it says,
if you redirect both standard output and standard error and then try to read both, for example using the following C# code.
[C#] string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit();... it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.
Or, I should use asynchronous read operations on at least one of the streams.
However, I still couldn't figure out how to catch both the standard output and standard error.
Please help, I need a
string output_with_error = ...
How can I do that?
Thanks
すべての返信
-
2012年4月10日 22:57モデレータ
Argh. Dusting off the cob webs here. The reason why it doesn't write the output until the process closes is because it doesn't close the stream, which flushes the buffers until the process closes. I'm doing a search for some code that works around this, with some limitations, by forcing the stream to close before the process closes.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
-
2012年4月11日 12:56Thanks Rudy. Looking forward to it.
-
2012年4月12日 21:57モデレータ
I have not been able to locate the old thread migrated from the old forums server.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
-
2012年4月13日 13:20
NP. Thank you all the same for your efforts.
I'll continue searching...- 編集済み smetah 2012年4月13日 13:20
-
2012年4月13日 13:59Process has events for "OutputDataReceived" and "ErrorDataReceived" There's an example of using asynchronous reading of output here. Doing the same for the error list is going to look pretty much exactly the same with just slightly different property/method names.
-
2012年4月13日 14:06Thanks!
Alright,
When asynchronous read operations start, the event handler is called each time the associated Process writes a line of text to its StandardOutput stream.
In that case, can I use the same event handler for both StandardOutput and StandardError stream?Will that create a race condition?
Thanks -
2012年4月13日 14:52
"In that case, can I use the same event handler for both StandardOutput and StandardError stream? "
You can the question is whether you want to or not.
"Will that create a race condition?"
That depends on what you do inside of the method. Could you make a race condition, yes. Could you prevent race conditions, also yes.
If you show us what you're doing here we could help with that, but just assume that the handler could be called concurrently.- 編集済み servy42Microsoft Community Contributor 2012年4月13日 14:53
-
2012年4月13日 14:54
All I wanted is to catch both StandardOutput and StandardError into the same string.
How can I do that?
thanks
-
2012年4月13日 15:01You should make two separate strings as stream destinations and then concatenate them together once both are read. The process might try to write to both streams at the same time, so you should be prepared for this situation.
-
2012年4月13日 15:18
Just make a StringBuilder to put the results in, and use a 'lock' block around the 'Append' method so that only one thread is writing to the SB at any given time.All I wanted is to catch both StandardOutput and StandardError into the same string.
How can I do that?
thanks
- 回答としてマーク smetah 2012年4月17日 13:36
-
2012年4月13日 15:21
Thanks. Will try to figure out what that translates into code...
-
2012年4月16日 5:30
Hi,
Does anybody find the solution?
-
2012年4月16日 13:56
Yes, and links to the examples were posted here. You can find several different examples on MSDN looking around the various properties/methods related to input/output redirection on MSDN.Hi,
Does anybody find the solution?
-
2012年4月17日 13:37
I gave up.Thanks. Will try to figure out what that translates into code...
-
2012年4月17日 13:55
Could you post what you had so far?
Did you have trouble subscribing to all of the events?
Did you have problems with the synchronization and therefore having race conditions?
Did you have trouble getting a StringBuilder to be shared between the two events?
Something else?
-
2012年4月17日 13:59
thanks for your inquiry.
I gave up going along this path because it's over and beyond my capability, and I found the following simple enough for me.
http://stackoverflow.com/
questions/1420965/redirect- stdout-and-stderr-to-a-single- file-in-dos - 回答としてマーク smetah 2012年4月17日 13:59

