Walkthrough: Debug a multithreaded app using the Threads window (C#, Visual Basic, C++)

Several Visual Studio user interface elements help y'all debug multithreaded apps. This article introduces multithreaded debugging features in the code editor window, Debug Location toolbar, and Threads window. For information about other tools for debugging multithreaded apps, see Go started debugging multithreaded apps.

Completing this tutorial takes simply a few minutes, and familiarizes y'all with the nuts of debugging multithreaded apps.

Create a multithreaded app project

Create the following multithreaded app project to employ in this tutorial:

  1. Open up Visual Studio and create a new project.

    If the start window is not open, choose File > Showtime Window.

    On the start window, choose Create a new project.

    On the Create a new projection window, enter or type panel in the search box. Next, choose C# or C++ from the Language list, and and so choose Windows from the Platform list.

    After you apply the linguistic communication and platform filters, choose the Panel App for .Internet Core or for C++, and so choose Next.

    Note

    If you don't see the correct template, get to Tools > Get Tools and Features..., which opens the Visual Studio Installer. Choose the .Cyberspace Cadre cross-platform evolution or Desktop development with C++ workload, then choose Modify.

    In the Configure your new project window, type or enter MyThreadWalkthroughApp in the Project name box. Then, choose Adjacent or Create, whichever choice is bachelor.

    For .NET Core, cull either the recommended target framework or .NET half dozen, and so choose Create.

    From the pinnacle menu bar, choose File > New > Project. In the left pane of the New project dialog box, choose the post-obit:

    • For a C# app, under Visual C#, choose Windows Desktop, and and so in the heart pane cull Console App (.NET Framework).
    • For a C++ app, under Visual C++, choose Windows Desktop,, and then choose Windows Console Application.

    If yous don't come across the Console App (.NET Framework) or, for C++, the Console App projection template, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. Choose the .NET desktop development or Desktop development with C++ workload, and then choose Modify.

    Then, blazon a name similar MyThreadWalkthroughApp and click OK.

    Select OK.

    A new console project appears. After the project has been created, a source file appears. Depending on the language you have chosen, the source file might be called Programme.cs, MyThreadWalkthroughApp.cpp, or Module1.vb.

  2. Supplant the code in the source file with the C# or C++ case code from Get started debugging multithreaded apps.

  3. Select File > Salve All.

Start debugging

  1. Find the post-obit lines in the source code:

                      Thread.Sleep(3000); Panel.WriteLine();                                  
                      Thread::Sleep(3000); Console.WriteLine();                                  
  2. Set a breakpoint on the Console.WriteLine(); line by clicking in the left gutter, or selecting the line and pressing F9.

    The breakpoint appears as a carmine circle in the left gutter next to the lawmaking line.

  3. Select Debug > Start Debugging, or press F5.

    The app starts in debug mode, and pauses at the breakpoint.

  4. While in break style, open the Threads window by selecting Debug > Windows > Threads. You must be in a debugging session to open or see the Threads and other debugging windows.

Examine thread markers

  1. In the source code, locate the Console.WriteLine(); line.

    1. Right-click in the Threads window, and select Show Threads in Source Show Threads in Source from the menu.

    The gutter adjacent to the source lawmaking line now displays a thread marker icon Thread Marker. The thread mark indicates that a thread is stopped at this location. If there is more than 1 stopped thread at the location, the multiple threads icon appears.

  2. Hover the arrow over the thread marker. A DataTip appears, showing the proper noun and thread ID number for the stopped thread or threads. The thread names may be <No Name>.

    Tip

    To help identify nameless threads, you can rename them in the Threads window. Right-click the thread and select Rename.

  3. Right-click the thread marking in the source code to see the available options on the shortcut menu.

Flag and unflag threads

You tin flag threads to go along rails of threads you desire to pay special attention to.

Flag and unflag threads from the source lawmaking editor or from the Threads window. Choose whether to display but flagged threads, or all threads, from the Debug Location or Threads window toolbars. Selections fabricated from whatever location affect all locations.

Flag and unflag threads in source code

  1. Open up the Debug Location toolbar by selecting View > Toolbars > Debug Location. You can as well correct-click in the toolbar area and select Debug Location.

  2. The Debug Location toolbar has three fields: Process, Thread, and Stack Frame. Drop down the Thread list, and notation how many threads there are. In the Thread list, the currently executing thread is marked by a > symbol.

  3. In the source lawmaking window, hover over a thread mark icon in the gutter and select the flag icon (or one of the empty flag icons) in the DataTip. The flag icon turns red.

    You tin also correct-click a thread marking icon, point to Flag, and so select a thread to flag from the shortcut carte.

  4. On the Debug Location toolbar, select the Testify But Flagged Threads icon Show Flagged Threads, to the right of the Thread field. The icon is grayed out unless 1 or more threads are flagged.

    Only the flagged thread now appears in the Thread dropdown in the toolbar. To show all threads again, select the Prove Simply Flagged Threads icon once more.

    Tip

    Later yous have flagged some threads, you can place your cursor in the lawmaking editor, right-click, and select Run Flagged Threads to Cursor. Make sure to choose lawmaking that all flagged threads will reach. Run Flagged Threads to Cursor will pause threads on the selected line of lawmaking, making it easier to control the lodge of execution by freezing and thawing threads.

  5. To toggle the flagged or unflagged status of the currently executing thread, select the single flag Toggle Current Thread Flagged Country toolbar button, to the left of the Bear witness Only Flagged Threads push button. Flagging the electric current thread is useful for locating the current thread when just flagged threads are showing.

  6. To unflag a thread, hover over the thread mark in the source code and select the red flag icon to clear information technology, or correct-click the thread marking and select Unflag.

Flag and unflag threads in the Threads window

In the Threads window, flagged threads take red flag icons next to them, while unflagged threads, if shown, accept empty icons.

Threads Window

Select a flag icon to change the thread state to flagged or unflagged, depending on its current state.

Y'all tin can also right-click a line and select Flag, Unflag, or Unflag All Threads from the shortcut carte du jour.

The Threads window toolbar also has a Show Flagged Threads But button, which is the correct-manus one of the two flag icons. It works the same as the button on the Debug Location toolbar, and either button controls the brandish in both locations.

Other Threads window features

In the Threads window, select the header of any column to sort the threads by that cavalcade. Select again to contrary the sort order. If all threads are showing, selecting the flag icon column sorts the threads by flagged or unflagged status.

The second cavalcade of the Threads window (with no header) is the Current Thread column. A yellow pointer in this column marks the current execution indicate.

The Location column shows where each thread appears in the source lawmaking. Select the expand arrow next to the Location entry, or hover over the entry, to show a partial phone call stack for that thread.

Tip

For a graphical view of the call stacks for threads, use the Parallel Stacks window. To open the window, while debugging, select Debug> Windows > Parallel Stacks.

In add-on to Flag, Unflag, and Unflag All Threads, the right-click context menu for Thread window items has:

  • The Bear witness Threads in Source push button.
  • Hexadecimal brandish, which changes the Thread IDs in the Threads window from decimal to hexadecimal format.
  • Switch To Thread, which immediately switches execution to that thread.
  • Rename, which lets you change the thread name.
  • Freeze and Thaw commands.

Freeze and thaw thread execution

You can freeze and thaw, or append and resume, threads to command the gild in which the threads perform work. Freezing and thawing threads can help y'all resolve concurrency bug, such as deadlocks and race conditions.

To freeze and unfreeze threads:

  1. In the Threads window, right-click any thread and so select Freeze. A Interruption icon in the Electric current Thread column indicates that the thread is frozen.

  2. Select Columns in the Threads window toolbar, so select Suspended Count to display the Suspended Count column. The suspended count value for the frozen thread is i.

  3. Right-click the frozen thread and select Thaw.

    The Pause icon disappears, and the Suspended Count value changes to 0.

Switch to another thread

Y'all may see a The application is in break fashion window when you try to switch to another thread. This window tells you that the thread does not accept whatsoever code that the current debugger can brandish. For case, y'all may be debugging managed lawmaking, but the thread is native code. The window offers suggestions for resolving the issue.

To switch to another thread:

  1. In the Threads window, brand a note of the current thread ID, which is the thread with a yellow pointer in the Current Thread cavalcade. You'll desire to switch back to this thread to continue your app.

  2. Right-click a unlike thread and select Switch To Thread from the context menu.

  3. Detect that the yellow arrow location has changed in the Threads window. The original current thread marker also remains, every bit an outline.

    Wait at the tooltip on the thread marker in the code source editor, and the list in the Thread dropdown on the Debug Location toolbar. Observe that the current thread has also changed there.

  4. On the Debug Location toolbar, select a different thread from the Thread listing. Notation that the current thread changes in the other ii locations likewise.

  5. In the source lawmaking editor, right-click a thread marking, signal to Switch To Thread, and select another thread from the listing. Observe that the electric current thread changes in all three locations.

With the thread marker in source lawmaking, you can switch merely to threads that are stopped at that location. By using the Threads window and Debug Location toolbar, you can switch to whatever thread.

You've at present learned the nuts of debugging multithreaded apps. You can observe, flag and unflag, and freeze and thaw threads by using the Threads window, the Thread list in the Debug Location toolbar, or thread markers in the source lawmaking editor.

Run into besides

  • Debug multithreaded applications
  • How to: Switch to another thread while debugging