1. Bind the console output to TextWriter
It is necessary to use the key attribute setout of Console. Setout requires the parameter TextWriter, so Write's information will be written to this textWriter. I use StringWriter, which can be bound to a StringBilder.
Stringbuilder outsb = newstringbuilder ();
StringWriter SW = New StringWriter (OUTSB);
Console.setOut (SW);
Console.WriteLine ("console test!");
TextBox1.text = outsb.tostring;
This way TextBox1 can display the information of Console Write. However, this can only display information for previous console.write in TextBox.
2. Display the information of Console.Write instantly
Here is a thread to surveillance, instantly display the information of console.write (). Refresh every 500 milliseconds.
System.Threading.ThreadPool.queueUserWorkItem (new system.threading.waitcallback (consoleout);
Void consoleout (Object objState)
{
While (True)
{
TextBox1.text = outsb.tostring;
Thread.sleep (500)
}
}
3. However, StringWriter will continue to increase with Write, which requires limiting the size of StringWriter. However, StringWriter does not have attributes to control other sizes, can only control their size by binding its StringBuilder.
System.Threading.ThreadPool.queueUserWorkItem (new system.threading.waitcallback (consoleout);
Void consoleout (Object objState)
{
While (True)
{
I = Outsb.length;
IF (i> 1024000) // Clear it when the size exceeds 1M
{
Outsb.remove (0, i);
}
TextBox1.text = outsb.tostring;
Thread.sleep (500)
}
}