Reputation: 7388
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?
Upvotes: 6
Views: 152
Reputation: 8131
DebugView is an application that lets you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. It is capable of displaying both kernel-mode and Win32 debug output, so you don't need a debugger to catch the debug output your applications or device drivers generate, nor do you need to modify your applications or drivers to use non-standard debug output APIs.
Upvotes: 0
Reputation: 82934
If you redirect the Console.Out
to an instance of a StringWriter
, you can then get the text that has been written to the console:
StringWriter writer = new StringWriter();
Console.SetOut(writer);
StringBuilder consoleOut = writer.GetStringBuilder();
string text = consoleOut.ToString();
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 a TextBox
. A crude example:
public MyForm()
{
InitializeComponent();
StringWriter writer = new StringWriter();
Console.SetOut(writer);
Timer timer = new Timer();
timer.Tick += (o, s) => textBox.Text = writer.GetStringBuilder().ToString();
timer.Interval = 500;
timer.Start();
}
A few things to be careful with:
StringWriter
is disposable, so technically you need to dispose of it when done (although in reality its Dispose()
method does nothing so not really a big issue).StringWriter
keeps an internal StringBuilder
containing 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.Out
back to its original value when you close your form, otherwise you will not be able to print messages to the console again.Upvotes: 5