I have some screens making some data queries that might take a long time. My db layer already puts the query strings to the console (I can see them in the output window when debugging). I just want to have a window the user can open to see the same thing if they grow impatient.
Any suggestions on a quick way to do this?
If you redirect the
Console.Outto an instance of aStringWriter, you can then get the text that has been written to the console:If you do this within a new
Form, you can then poll at an interval to get the text that has been written to the console so far and put its value into aTextBox. A crude example:A few things to be careful with:
StringWriteris disposable, so technically you need to dispose of it when done (although in reality itsDispose()method does nothing so not really a big issue).StringWriterkeeps an internalStringBuildercontaining all of the text written to it so far. This will only ever get bigger over time, so the longer your application runs, the more memory it will consume. You could put some checks in to clear it out periodically when it reaches a certain size.Console.Outback to its original value when you close your form, otherwise you will not be able to print messages to the console again.