Redirecting Console and Debug Output to a textbox.
_consoleStreamWriter = new TextBoxStreamWriter(txtConsoleOutput, tabConsole);
_debugStreamWriter = new TextBoxStreamWriter(txtDebugOutput, tabDebugPage);
TextWriterTraceListener writer = new TextWriterTraceListener(_debugStreamWriter);
Debug.Listeners.Add(writer);
Debug.WriteLine("Now redirecting debug output.");
// Redirect the out Console stream
Console.SetOut(_consoleStreamWriter);
Console.WriteLine("Now redirecting console output.");
To prevent application crash when writing to the Textbox (which will happen with the default code found elsewhere), do the below:
public class TextBoxStreamWriter : TextWriter
{
private StringBuilder messageBuilder = new StringBuilder();
private TextBox _output = null;
private Control _parentControl = null;
private System.Timers.Timer _displayTimer = new System.Timers.Timer();
public TextBoxStreamWriter(TextBox output, Control parentControl)
{
_output = output;
_parentControl = parentControl;
_displayTimer.Interval = 1000;
_displayTimer.Elapsed += new System.Timers.ElapsedEventHandler(_displayTimer_Elapsed);
_displayTimer.Start();
}
public void DisableOperation()
{
if (_displayTimer != null)
{
_displayTimer.Elapsed -= new System.Timers.ElapsedEventHandler(_displayTimer_Elapsed);
_displayTimer.Stop();
}
}
void _displayTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ShowMessage();
}
public override void Write(char value)
{
base.Write(value);
lock (messageBuilder)
{
messageBuilder.Append(value.ToString());
}
}
public delegate void ShowMessageDelegate();
private void ShowMessage()
{
if (messageBuilder.Length < 1)
{
return;
}
if (_output.InvokeRequired)
{
_output.Invoke(new ShowMessageDelegate(ShowMessage));
}
else
{
_output.AppendText(messageBuilder.ToString());
lock (messageBuilder)
{
messageBuilder.Remove(0, messageBuilder.Length);
}
}
}
}
_debugStreamWriter = new TextBoxStreamWriter(txtDebugOutput, tabDebugPage);
TextWriterTraceListener writer = new TextWriterTraceListener(_debugStreamWriter);
Debug.Listeners.Add(writer);
Debug.WriteLine("Now redirecting debug output.");
// Redirect the out Console stream
Console.SetOut(_consoleStreamWriter);
Console.WriteLine("Now redirecting console output.");
To prevent application crash when writing to the Textbox (which will happen with the default code found elsewhere), do the below:
public class TextBoxStreamWriter : TextWriter
{
private StringBuilder messageBuilder = new StringBuilder();
private TextBox _output = null;
private Control _parentControl = null;
private System.Timers.Timer _displayTimer = new System.Timers.Timer();
public TextBoxStreamWriter(TextBox output, Control parentControl)
{
_output = output;
_parentControl = parentControl;
_displayTimer.Interval = 1000;
_displayTimer.Elapsed += new System.Timers.ElapsedEventHandler(_displayTimer_Elapsed);
_displayTimer.Start();
}
public void DisableOperation()
{
if (_displayTimer != null)
{
_displayTimer.Elapsed -= new System.Timers.ElapsedEventHandler(_displayTimer_Elapsed);
_displayTimer.Stop();
}
}
void _displayTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ShowMessage();
}
public override void Write(char value)
{
base.Write(value);
lock (messageBuilder)
{
messageBuilder.Append(value.ToString());
}
}
public delegate void ShowMessageDelegate();
private void ShowMessage()
{
if (messageBuilder.Length < 1)
{
return;
}
if (_output.InvokeRequired)
{
_output.Invoke(new ShowMessageDelegate(ShowMessage));
}
else
{
_output.AppendText(messageBuilder.ToString());
lock (messageBuilder)
{
messageBuilder.Remove(0, messageBuilder.Length);
}
}
}
}
Comments
Post a Comment