5 Tips for Debugging Multi-threaded Code in Visual Studio.Net

I have been talking about threading and threading patterns for a while now but somehow fell short on how to debug them. Today we will look into tips as well as tools available in the Visual Studio IDE that enables us to effectively debug multi-threaded applications like non-threaded ones.

Tip #1: Always Name Your Threads

Whenever you create a thread, remember to always give a name to it. You can give a thread a name by assigning a string value to the Name property. Below is a sample code on how to give a thread a name as taken from the constructor of WorkerThreadBase.

    1 protected WorkerThreadBase(string name,

    2     ThreadPriority priority,

    3     bool isBackground)

    4 {

    5     _disposing = false;

    6     _disposed = false;

    7     _stopping = new ManualResetEvent(false);

    8     _stopped = new ManualResetEvent(false);

    9 

   10     _workerThread = new Thread(threadProc);

   11     _workerThread.Name = name == null ? GetType().Name : name;

   12     _workerThread.Priority = priority;

   13     _workerThread.IsBackground = isBackground;

   14 }

Tip #2: Use the Threads Debug Window

You can open this window by clicking Debug->Windows->Threads or Ctrl+D, T in a debug session. In this window you can see the number of threads open for your application. You can also switch between threads by double clicking on a line in the table.

Below is picture of the Threads Debug Window

Tip #3: Use the Call Stack Debug Window

You can open this window by clicking Debug->Windows->Threads or Ctrl+D, C in a debug session. In this window you can see the different methods that got called before reaching the current breakpoint location. Each line in this table corresponds to a Stack Frame. You can double click a line in this window to view the corresponding method in that stack frame.

Below is picture of the Call Stack Debug Window

Tip #4: Use the Debug Location Toolbar

You can open the debug location toolbar by clicking View->Toolbars->Debug Location. This toolbar is the one that displays the Process, Thread and Stack Frame of the method being debugged.

You can also use this toolbar to switch between the live threads and navigate the call stack of the selected thread just like the Threads Debug Window and Call Stack Debug Window.

Below is picture of the Debug Location Toolbar for your reference.

Tip #5: Use Conditional Breakpoints to Simulate Single-threaded Execution

Choose strategic points where you want to inspect the state of the thread. Create breakpoints in the selected locations. On each of the breakpoints, click Right-Mouse-Button->Condition. In the textbox where you are supposed to enter a condition, enter

System.Threading.Thread.CurrentThread.Name = "<THREAD_NAME>".

Where <THREAD_NAME> is equal to the name of the thread you wish to inspect.

Below is picture of the Breakpoint Dialog for your reference.

Comments

# cruizer said:

thanks for the tips. i noticed though that sometimes delicate threading code works differently when run in Debug mode via the IDE and when run directly from Windows Explorer...

Friday, January 09, 2009 8:06 PM