Click here to Skip to main content
15,881,173 members
Articles / .NET

Switching between filtered threads

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Jul 2012CPOL2 min read 10.5K   4  
About switching between filtered threads

Introduction

Threads dropdown in Debug Location Toolbar provide a quick way of switching between various threads and observe details about what lines of code are currently been executed by those threads. If you don’t know which control I am referring to, the figure below shows threads dropdown in debug location toolbar.

Image 1

In case you don’t see this dropdown in your Visual Studio toolbar, make sure that you have Debug Locations toolbar checked in the toolbar context menu as shown below:

Image 2

If your application has many threads and you need to focus on few threads at a given time, then finding those threads out of a long list and switching between these could become a bit cumbersome. Let’s take a look at the code to describe what exactly I am talking about.

C#
static void Main(string[] args)
{
    Thread[] workers = new Thread[25];
    for (int i = 0; i < workers.Length; i++)
    {
        if (i > 0 && i % 21 == 0)
            workers[i] = new Thread(new ThreadStart(Method1));
        else if (i > 0 && i % 23 == 0)
            workers[i] = new Thread(new ThreadStart(Method2));
        else
            workers[i] = new Thread(new ThreadStart(Method3));
        workers[i].Name = "Thread" + i.ToString();
        workers[i].Start();
    }
    Console.WriteLine("Thread Creation Completed.");

    for (int i = 0; i < workers.Length; i++)
        workers[i].Join();

    Console.WriteLine("Done.");
}

private static void Method1()
{
    Thread.Sleep(10000);
}

private static void Method2()
{
    Thread.Sleep(30000);
}

private static void Method3()
{
    Thread.Sleep(10000);
}

In this code, we have instantiated twenty-five threads. Out of these, one thread will be executing Method1, another one will be executing Method2 while other twenty-three threads will be executing Method3. Let’s run this code in debug mode, and put a breakpoint on the following line of code:

C#
Console.WriteLine("Thread Creation Completed.");

Let’s assume that there is some issue that you need to debug and it requires to switch between threads that involves Method1 and Method2. At this point, if you inspect the Threads dropdown in Debug Location toolbar, you will see that all twenty-five threads are listed here and switching between those two threads require to scroll the window, find right threads, etc. To keep doing this could be quite cumbersome.

Image 3

In order to simplify this switching between threads involving Method1 and Method2 only, you can go in Threads window and select the flag in the left most columns for threads that are executing Method1 and Method2.

Image 4

Once you flag threads of your interest, you click on "Show Only Flagged Threads" button in the toolbar as shown below:

Image 5

Once this button is enabled, you will only see the flagged threads in the Threads window as shown below:

Image 6

Hopefully 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 --