Introduction
This article is mainly focused on the process of multithread debugging. We have used thread in C# many times, and we usually 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 in C#
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 is a thread in C#?
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 where I created 2 threads.
using System;
using System.Threading;
namespace DebugThread
{
class Program
{
static int i = 0;
static void Main(string[] args)
{
Thread T1 = new Thread(MyMethod);
Thread T2 = new Thread(MyMethod);
T1.Start();
T2.Start();
Console.Read();
}
static void MyMethod()
{
for (int j = 0; j < 10; j++) // Changed the loop variable name to avoid conflict
{
i++; // Note: This will increment the shared 'i' variable
Thread.Sleep(5000);
}
}
}
}
Open the Thread Window in C#
Step 1. Insert a Breakpoint in the code.
static void MyMethod()
{
for (int i = 0; i < 10; i++)
{
i++; // Increment 'i'
Thread.Sleep(5000); // Sleep for 5000 milliseconds (5 seconds)
}
}








Safayat ZisanPosted Apr 25, 2015, 12:16 AM
good one
Raj KumarPosted Apr 24, 2015, 1:59 AM
Good one rajeev...keep it up !
Khargesh RajputPosted Apr 24, 2015, 1:09 AM
nice article
Manoj BhoirPosted Apr 24, 2015, 12:57 AM
Well explained.....
Shridhar SharmaPosted Apr 23, 2015, 3:29 PM
useful resource Rajeev Ranjan sir :)
Karthik Muthu KaruppanPosted Apr 23, 2015, 11:14 AM
good one
Gowtham RajamanickamPosted Apr 23, 2015, 3:34 AM
good one
Gowtham RajamanickamPosted Apr 23, 2015, 3:34 AM
good one
NitinPosted Apr 23, 2015, 2:34 AM
well explained
Praveen KumarPosted Apr 23, 2015, 1:46 AM
Nicely explained !!! Rajeev Ranjan