Introduction
While working with multi-threaded applications, sometime when either application has been paused or when a breakpoint got hit, you may need to see what code is been executed by various threads and how it’s related to other running threads. This could be accomplished by a Visual Studio feature called “Show Threads in Source”. This feature could be enabled by two ways. One way of enabling it is by clicking on the “Show Threads in Source” button available in the Debug Toolbar button as shown in the figure below:
Another way of enabling it is by right clicking in the Threads windows and selecting Show Threads in Source as shown below:
Now when your application hits a breakpoint, and if you have this option enabled, you should see squiggly lines in the left bar under Visual Studio. Once you hover the mouse over these squiggly lines, you should be able to see which threads are currently executing that particular line of code.
Enough theory, let’s see this trick in action. Let’s say we have the following code. It's a very simple code, but enough to explain this trick.
class Program
{
static object sync1 = new object();
static object sync2 = new object();
static void Main(string[] args)
{
Thread[] workers = new Thread[10];
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
workers[i] = new Thread(new ThreadStart(Method1));
workers[i].Start();
}
else
{
workers[i] = new Thread(new ThreadStart(Method2));
workers[i].Start();
}
}
Console.WriteLine("Thread Creation Completed.");
for (int i = 0; i < 10; i++)
{
workers[i].Join();
}
Console.WriteLine("Done");
}
private static void Method1()
{
Console.WriteLine("Method1 called by ThreadId = {0}",
Thread.CurrentThread.ManagedThreadId);
lock (sync1)
{
Thread.Sleep(10000);
}
}
private static void Method2()
{
Console.WriteLine("Method2 called by ThreadId = {0}",
Thread.CurrentThread.ManagedThreadId);
lock (sync2)
{
Thread.Sleep(30000);
}
}
}
Nothing complex in this code. We have instantiated ten threads. Out of these ten threads, five are running Method1
and the other five are running Method2
. In each of these two methods, we have a Sleep
under a lock
statement. Now, I have set a breakpoint on the following line of code:
Console.WriteLine("Thread Creation Completed.");
At this point, enable the "Show Threads in Source option" and run the application. Once breakpoint get hit, we can inspect various threads and how its related to other threads. The figure below shows the thread window. I have applied the trick described here to flag threads from this application.
Now if you notice in Visual Studio around Method1
and Method2
, you should see few squiggly lines in the left bar with code in those lines in gray as shown below:
Let's hover the mouse over the first squiggly line in Method1
. At this point, you should see the threads with their IDs that are currently been on that particular line. As shown below, we have four threads waiting on the lock
statement to get their turn on acquiring this lock.
If you hover mouse over the Thread.Sleep
statement within Method1
, you will see one thread that is running a particular statement as shown below:
Similarly, if you hover the mouse over two squiggly lines in Method2
, you can find the information about threads that are being executed on those two lines.

I hope you will find this tip helpful.