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.
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:
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.
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:
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.
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
.
Once you flag threads of your interest, you click on "Show Only Flagged Threads" button in the toolbar as shown below:
Once this button is enabled, you will only see the flagged threads in the Threads window as shown below:
Hopefully you will find this tip helpful.
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