2009-04-20 07:20:25 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Examples
|
|
|
|
|
{
|
|
|
|
|
public class TextBoxTraceListener : TraceListener
|
|
|
|
|
{
|
|
|
|
|
private TextBox _target;
|
|
|
|
|
private StringSendDelegate _invokeWrite;
|
|
|
|
|
|
|
|
|
|
public TextBoxTraceListener(TextBox target)
|
|
|
|
|
{
|
|
|
|
|
_target = target;
|
|
|
|
|
_invokeWrite = new StringSendDelegate(SendString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Write(string message)
|
|
|
|
|
{
|
2009-05-30 17:51:19 +00:00
|
|
|
|
if (_target.Created)
|
|
|
|
|
_target.Invoke(_invokeWrite, new object[] { message });
|
2009-04-20 07:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void WriteLine(string message)
|
|
|
|
|
{
|
2009-05-30 17:51:19 +00:00
|
|
|
|
if (_target.Created)
|
|
|
|
|
_target.Invoke(_invokeWrite, new object[] { message + Environment.NewLine });
|
2009-04-20 07:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private delegate void StringSendDelegate(string message);
|
|
|
|
|
private void SendString(string message)
|
|
|
|
|
{
|
2009-05-30 17:51:19 +00:00
|
|
|
|
if (_target.Created)
|
|
|
|
|
{
|
|
|
|
|
// No need to lock text box as this function will only
|
|
|
|
|
// ever be executed from the UI thread
|
|
|
|
|
_target.Text += message;
|
|
|
|
|
}
|
2009-04-20 07:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|