How to Debug Multithreaded Programs in C#


This article is mainly focused on the process of multithread debugging. We have used thread in C# many times and we ususally ignore the thread debugging process, usually we only check the coding part and trace the error. As we know threading problems are difficult to handle. Like we have an immutable application that handles the many mutable objects (single thread). If we can see the problem of deadlocks, usually a deadlock is the easiest problem to debug. Let's suppose we get a deadlock error in a stack trace. We can easily fix our code and prevent that error. With deadlocks, the problem is the same locks in different orders. Now let's see some other aspects of threads.

Race conditions

A race condition is a difficult problem to debug and also a harder task to find from our code. Some other issues make our application cumbersome to handle.

Perhaps this article can help you if you are unfamiliar with the new thread window. In Visual Studio 2012 there is an improvised Thread window. In that Thread window we can debug our multithreaded applications easily. So before we start just try to work through the following topics that we will cover.

  • What threads are.
  • Working with multithreaded console applications.
  • Open the Thread Window
  • Working with the Thread Window

What a thread is

A thread is the smallest sequence of programmed instructions, or we can say that a thread is a small set of executable instructions that can be used to isolate a task from a process. And in some cases we can say that a thread is a component of a process.

We can use threads in C# using the System.Threading namespace.

Working with multiple threads in console applications

In this section we will see a simple console application, were I created 2 threads.

  1. namespace DebugThread  
  2. {  
  3.     class Program  
  4.     {  
  5.         static int i = 0;  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Thread T1 = new Thread(MyMethod);  
  9.             Thread T2 = new Thread(MyMethod);  
  10.             T1.Start();  
  11.             T2.Start();  
  12.             Console.Read();  
  13.         }  
  14.         static void MyMethod()  
  15.         {  
  16.             for (int i = 0; i < 10; i++)  
  17.             {  
  18.                 i++;  
  19.                 Thread.Sleep(5000);  
  20.             }  
  21.         }  
  22.     }  
  23. }  

Open the Thread Window

Step 1

Insert a Breakpoint in the code.


Step 2

Run our application (press F5).

Step 3

Click on the Debug menu and choose the windows option then select Threads as shown in the following image.


Working with Thread Window

Now let's have a look at the Thread window.

As in the preceding image we can see the thread having no name. So, let's assign the name of the thread.


Now we can see the thread name in Thread Window.


Flagging and Unflagging Thread

  • Go to the Debugging Location toolbar and click the Thread list and see how many threads appear in the list.

  • Go back to the source window and right-click the Thread marker again.

  • On the shortcut menu, point to Flag and then click the thread name and ID number.

To unflag threads

On the Threads window, right-click the line corresponding to the flagged thread. A shortcut menu is displayed. It has options to Unflag and Unflag All.

To unflag the thread, click Unflag. Or click the Red flag icon.


To freeze and Thaw threads

Freezing a thread will suspend its execution, in the debugging process. We can say Freeze has suspended the process and Thawing resumes the process. By Freezing and Thawing threads (suspending and resuming), we can have control of the threads running during the debugging process. It helps us when we are solving bugs related to concurrency.

In the Threads window, right-click any thread and then click Freeze.


Look at the active thread column. The pair of vertical bars now appear there.

Right-click the frozen thread and then click Thaw.


The active thread column and the Suspend column change.

Similar Articles