none
Redirect standard output of a spawned thread RRS feed

  • Question

  • Hi all, I'm creating a thread in my program that outputs console messages. I want to be able to collect this output in my main thread instead of in the spawned thread. I know this can be easily done with processes, but I've never tried to do it for a thread and all my google queries pertained to processes rather than threads. Is this something that can be easily done? Thanks for any input.

    Also, just to give some people some insight into why I need this, I'm using a background worker to basically do a bunch of file conversions. One of the conversion techniques involves calling another class's method. This method's console output will basically give me a way to represent how long the conversion process may take so I can portray that in my GUI (rather than this console window from the class).
    Thursday, March 19, 2009 6:49 PM

Answers

  • A thread does not have its own console; it is just writing to the process console.

    You can redirect the stdout for the thread by calling Console.SetOut. Be aware, though, that this sets the console output for every thread in your process, not just that one.

           -Steve
    • Proposed as answer by Harry Zhu Tuesday, March 24, 2009 8:00 AM
    • Marked as answer by Harry Zhu Thursday, March 26, 2009 12:43 AM
    Monday, March 23, 2009 1:27 PM

All replies

  • Hi,

    What does the worker do ? What is the method's console output?
    Could you post the code you are working with?

    Harry
    http://cfx.codeplex.com where you can find samples you are looking for
    Monday, March 23, 2009 10:24 AM
  • A thread does not have its own console; it is just writing to the process console.

    You can redirect the stdout for the thread by calling Console.SetOut. Be aware, though, that this sets the console output for every thread in your process, not just that one.

           -Steve
    • Proposed as answer by Harry Zhu Tuesday, March 24, 2009 8:00 AM
    • Marked as answer by Harry Zhu Thursday, March 26, 2009 12:43 AM
    Monday, March 23, 2009 1:27 PM