Debugging Multi-Threaded Applications II – Setting A Thread’s Name Using The Debugger

Naming your application's threads can make debugging multi-threaded applications quite a bit simpler, especially if you have more than a couple of threads.  Thread names can be set in code as shown below:

// set the current thread's name
Thread.CurrentThread.Name = "UserInterfaceThread";

// create several worker threads and give them names
for(int i = 0; i < this.maxThreads; i++)
    Thread worker = new Thread(new ThreadStart(Worker));
    worker.Name = String.Format("WorkerThread{0}", i);

If you find yourself debugging a multi-threaded application that does not have names assigned to the threads, you can use Visual Studio to give them names.

While at a breakpoint, set the current thread's name, using the Watch window:

  1. In the Name column, add Thread.CurrentThread.Name

  2. In the Value column, enter the desired thread name (ex: "BackgroundCalculationThread")

You can also set the name of a thread for which your app holds a reference:

  1. Locate the desired thread variable in the Autos or Locals window

  2. Expand the variable and locate the Name property

  3. Enter the desired thread name in the Value column


  • It is important to remember that once a thread is named, it's name cannot be changed.  Once set, any attempt to change the name of a thread will result in an InvalidOperationException being thrown.

  • For .NET Compact Framework developers, the Thread.Name property is supported starting with version 2.

By setting the thread names while debugging, it becomes very easy to find the threads you are looking for (ex: BackgroundCalculationThread and UserInterfaceThread) without having to walk the list of threads or writing down the thread ID numbers.  This is very useful when debugging an application with a large number of threads.

-- DK

This posting is provided "AS IS" with no warranties, and confers no rights.
Some of the information contained within this post may be in relation to beta software. Any and all details are subject to change.

Comments (0)

Skip to main content