65.9K
CodeProject 正在变化。 阅读更多。
Home

一个简单的 TextBox TraceListener

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.98/5 (18投票s)

2007 年 10 月 24 日

MIT

2分钟阅读

viewsIcon

91952

downloadIcon

2402

一个 TraceListener 类, 允许您在 Windows UI 测试工具中查看跟踪结果

Screenshot - TextBoxTraceListener.jpg

引言

这是一个简单的自定义 TraceListener 类,允许文本框在多线程应用程序中显示跟踪结果。

背景

我正在为我的一个服务编写测试框架,并认为有人可能已经发布了一个 TextBoxTraceListener 类来节省我的五分钟时间,但不幸的是,Google 搜索没有结果。 我能看到的唯一示例不是线程安全的,并且需要将所有代码都在 Windows UI 线程上执行——这在你运行服务测试框架时不太理想。 所以我就把它拼凑起来,并把它发布出来供大家重用。 实际上,这是一个非常简单的类,只是为了节省人们一点时间。

Using the Code

只需将 TextBoxTraceListener 类添加到你的代码中,然后创建监听器的实例(在构造函数中传递你想要输出跟踪的文本框),然后将你的跟踪监听器添加到 Trace Debug。 示例测试框架应该能让你很好地了解该怎么做,但它可能看起来像这样(其中 txtDisplayTrace 是表单上的一个文本框)

public partial class Form1 : Form
{
    TextBoxTraceListener _textBoxListener;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _textBoxListener = new TextBoxTraceListener(txtDisplayTrace);
        Trace.Listeners.Add(_textBoxListener);
    }
}

关注点

我很少有机会做需要 UI 的事情,所以经常忘记你不能从不同的线程访问控件,直到编译并出现错误(然后之前痛苦的记忆涌上心头)。 当然,要解决这个问题,你只需要在目标控件上调用 Invoke ,并传递一个委托到你想要执行的方法,这几乎就是 TextBoxTraceListener 所做的一切;扩展 TraceListener 类并调用更新方法(代码文件中有注释,我已从此处删除)。

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)
    {
        _target.Invoke(_invokeWrite, new object[] { message });
    }

    public override void WriteLine(string message)
    {
        _target.Invoke(_invokeWrite, new object[] 
            { message + Environment.NewLine });
    }

    private delegate void StringSendDelegate(string message);
    private void SendString(string message)
    {
        // No need to lock text box as this function will only 

        // ever be executed from the UI thread

        _target.Text += message;
    }
}

希望有人会发现它对节省五分钟的编码时间有用。

© . All rights reserved.