Click here to Skip to main content
Click here to Skip to main content
Go to top

A Simple TextBox TraceListener

, 24 Oct 2007
Rate this:
Please Sign up or sign in to vote.
A tracelistener class that allows you to view the results of the trace in your Windows UI test harness
Screenshot - TextBoxTraceListener.jpg

Introduction

This is a simple custom TraceListener class to allow text boxes to show the result of the trace in a multi-threaded application.

Background

I was writing a test harness for one of my services and figured someone would have already published a TextBoxTraceListener class to save me five minutes, but unfortunately Google came up short. The only examples I could see weren't threadsafe and would have needed all the code to be executed on the Windows UI thread - not exactly great when you're running a service test harness. So I figured I'd throw one together and put it out there for re-use. It's a very simple class really just to save people a bit of time.

Using the Code

Just add the TextBoxTraceListener class to your code, then create an instance of the listener (passing in the textbox you want to output the trace to in the constructor), then add your trace listener to Trace or Debug. The sample test harness should give you a pretty good idea of what to do, but it's probably going to look something like this (where txtDisplayTrace is a text box on the form):

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);
    }
}

Points of Interest

I very rarely get to do anything that requires a UI, so I frequently forget that you can't access controls from a different thread until I compile and get an error (then the memory of previous pains comes flooding back). Of course to solve this issue, all you have to do is call Invoke on the control in question and pass it a delegate to the method you want to execute, which is pretty much all that the TextBoxTraceListener does; extend the TraceListener class and invoke the update method (the code file has comments, which I've stripped from here).

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;
    }
}

Hopefully someone out there will find it useful for saving five minutes of coding.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Adam Crawford
Web Developer
England England
Adam lives in York and is just starting a new job as a senior web developer for a major travel company in the UK. He holds an MCPD for Web Development in ASP.NET 2 and has an interest in further developing his AJAX skills in the near future and will be practicing by developing his own site: Solid Code

Comments and Discussions

 
QuestionGreat!! PinmemberJan Navarro11-Jun-14 1:10 
GeneralThanks for the complete solution PinmemberVolodymyr tvv Tkachenko29-Oct-09 4:39 
Generalmy 2 cents PinmemberSPENNER23-Oct-09 5:51 
General5 minutes [modified] PinmemberBigTimeDonkey12-Oct-08 14:37 
QuestionThreadsave? Pinmemberwotina31-Mar-08 3:28 
Generalscroll to end Pinmembercanozurdo1-Nov-07 3:43 
GeneralRe: scroll to end PinmemberAdam Crawford1-Nov-07 15:15 
GeneralRe: scroll to end PinmemberIntarwebmaster14-Nov-07 4:25 
GeneralRe: scroll to end PinmemberAdam Crawford14-Nov-07 22:18 
GeneralNewbi question from an old timer PinmemberBillInPA31-Oct-07 6:56 
GeneralRe: Newbi question from an old timer Pinmemberfwd31-Oct-07 22:24 
Generalhave you heard about.... PinmemberOllie26-Oct-07 5:09 
GeneralRe: have you heard about.... PinmemberAdam Crawford26-Oct-07 5:25 
GeneralRe: have you heard about.... PinmemberOllie27-Oct-07 0:25 
GeneralRe: have you heard about.... PinmemberAdam Crawford27-Oct-07 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 24 Oct 2007
Article Copyright 2007 by Adam Crawford
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid