Walkthrough: Debugging a Multithreaded Application
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here
Visual Studio 2012 provides an improved Threads window and other user interface improvements to make it easier to debug multithreaded applications. This walkthrough takes only a few minutes, but completing it will familiarize you with the new interface features for debugging multithreaded applications.
To begin this walkthrough, you need a multithreaded application project. Follow the steps listed here to create that project.
To create the walkthrough project
On the File menu, choose New and then click Project.
The New Project dialog box appears.
In the Project Types box, click the language of your choice: Visual Basic, Visual C#, or Visual C++.
In the Templates box, choose Console Application or CLR Console Application.
In the Name box, type the name MyThreadWalkthroughApp.
A new console project appears. When the project has been created, a source file appears. Depending on the language you have chosen, the source file might be called Module1.vb, Program.cs, or MyThreadWalkthroughApp.cpp
Delete the code that appears in the source file and replace it with the example code that appears in the section "Creating a Thread" of the topic Creating Threads and Passing Data at Start Time.
On the File menu, click Save All.
To begin the walkthrough
In the source window, look for the following code:
To start debugging
Console.WriteLinestatement, point to Breakpoint and then click Insert Breakpoint.
In the gutter on the left side of the source window, a red ball appears. This indicates that a breakpoint is now set at this location.
On the Debug menu, click Start Debugging.
Debugging starts, your console application starts to run, and then stops at the breakpoint.
If the console application window has focus at this point, click in the Visual Studio window to return focus to Visual Studio.
In the source window, locate the line that contains the following code:
To discover the thread marker
Right-click in the Threads window, then click Show Threads in Source.
Look at the gutter on the left side of the window. On this line, you will see an icon that resembles two cloth threads. One thread is red and the other is blue. The thread marker indicates that a thread is stopped at this location. Possibly, thread is stopped at this location.
Hover the pointer over the thread marker. A DataTip that appears. The DataTip tells you the name and thread ID number for each stopped thread. In this case, there is only one thread, whose name is probably
Right-click the thread marker. Note the choices on the shortcut menu.
This icon is a thread marker:
Flagging and Unflagging Threads
In Visual Studio 2008, you can flag threads that you want to give special attention. Flagging threads is a good way to keep track of important threads and ignore threads you do not care about.
To flag threads
On View menu, point to Toolbars.
Make sure that the Debug Location toolbar is selected.
Go to the Debugging Location toolbar and click the Thread list.
You can recognize this toolbar by three prominent lists: Process, Thread, and Stack Frame.
Notice how many threads appear in the list.
Go back to the source window and right-click the Thread marker again.
On the shortcut menu, point to Flag, and then click the thread name and ID number.
Go back to Debugging Location toolbar and click the Thread list again.
Only the flagged thread appears in the list now. The flag button that is just to the right of the Thread list. The flag icon on the button was dimmed before. Now, it is a solid, bright red.
Hover the pointer over the flag icon.
A pop-up appears. This pop-up tells you what mode the Thread list is in: Show Only Flagged Threads.
Click the flag button to toggle back to Show All Threads mode.
Click the Thread list again and verify that you can now see all threads again.
Click the flag button to toggle back to Show Only Flagged Threads.
On the Debug menu, point to Windows and then click Threads.
The Threads window appears. One thread has a prominent flag icon attached to it.
In the source window, right-click the thread marker again.
Notice what choices are now available on the shortcut menu. Instead of Flag, you now see Unflag. Do not click Unflag.
Go to the next procedure about how to unflag threads.
To unflag threads
On the Threads window, right-click the line corresponding to the flagged thread.
A shortcut menu is displayed. It has options to Unflag and Unflag All.
To unflag the thread, click Unflag.
Click the red flag icon.
Look at the Debugging Location toolbar again. The flag button is dimmed again. You unflagged the only flagged thread. Because there are no flagged threads, the toolbar has gone back to Show All Threads mode. Click the Thread list and verify that you can see all threads.
Go back to the Threads window and examine the information columns.
At the top of each column, most of the buttons have titles that identify the column. However, The first column on the left has no title. Instead, it has an icon, which is the outline of a flag. You will notice the same outline in each row of the thread list. The outline means that the thread is unflagged.
Click the flag outlines for two threads, the second and third from the bottom of the list.
The flag icons become solid red, instead of hollow outlines.
Click the button at the top of the flag column.
The order of the thread list changed when you clicked the button. The thread list is now sorted with the flagged threads on top.
Again, click the button at the top of the flag column.
The sort order changed again.
More about the Threads window
To learn more about the Threads window
In the Threads window, examine the third column from the left. The button at the top of this column says ID.
The thread list is now sorted by thread ID number.
Right-click any thread in the list. On the shortcut menu, click Hexadecimal display.
The format of the thread ID numbers is changed.
Hover the mouse pointer over any thread in the list.
After a momentary delay, a DataTip appears. It shows a partial call stack for the thread.
Look at the fourth column from the left, which is labeled Category. The threads are classified into categories.
The first thread created in a process is referred to as the main thread. Locate it in the thread list.
Right-click the main thread and then click Switch to Thread.
A warning dialog box appears. It tells you that Visual Studio cannot display source code for the main thread.
Look at the Call Stack window and the Debug Location toolbar.
The contents of the Call Stack window have changed.
Switching the Active Thread
To switch threads
In the Threads window, examine the second column from the left. The button at the top of this column has no text or icon. This column is the Active Thread column.
Look at the Active Thread column and notice that one thread has a yellow arrow. This is the active thread indicator.
Make a note of the thread ID number where the active thread indicator is located. You will move the active thread indicator to another thread, but you will have to put it back when you have finished.
Right-click another thread and then click Switch to Thread.
Look at the Call Stack window in the source window. The contents have changed.
Look at the Debug Location toolbar. The active thread has changed there, too.
Go to the Debug Location toolbar. Click the Thread box and choose a different thread from the drop-down list.
Look at the Threads window. The active thread indicator has changed.
In the source window, right-click a thread marker. On the shortcut menu, point to Switch to and click a thread name/ID number.
You have now seen three ways of changing the active thread: using the Threads window, the Thread box in the Debug Location toolbar, and the thread indicator in the source window.
With the thread indicator, you can switch only to threads that are stopped at that particular location. By using the Threads window and Debug Location toolbar, you can switch to any thread.
Freezing and thawing thread execution
To freeze and unfreeze threads
In the Threads window, right-click any thread and then click Freeze.
Look at the active thread column. The pair of vertical bars now appear there. Those two blue bars indicate that the thread is frozen.
Look at the Suspend column. The suspend count for the thread is now 1.
Right-click the frozen thread and then click Thaw.
The active thread column and the Suspend column change.