Click here to Skip to main content
15,886,046 members
Articles / .NET
Tip/Trick

Show Threads in Source

Rate me:
Please Sign up or sign in to vote.
4.91/5 (4 votes)
28 Jul 2012CPOL2 min read 22K   7  
Find out which threads are running what line of code and how they may be related to other running threads

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:

Image 1

Another way of enabling it is by right clicking in the Threads windows and selecting Show Threads in Source as shown below:

Image 2

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.

C++
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:

C++
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.

Image 3

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:

Image 4

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.

Image 5

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:

Image 6

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. 

Image 7

Image 8

I hope you will find this tip helpful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

Comments and Discussions

 
-- There are no messages in this forum --