Reader small image

You're reading from  Parallel Programming and Concurrency with C# 10 and .NET 6

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781803243672
Edition1st Edition
Right arrow
Author (1)
Alvin Ashcraft
Alvin Ashcraft
author image
Alvin Ashcraft

Alvin Ashcraft is a software engineer and developer community champion with over 25 years of experience in software development. Working primarily with Microsoft Windows, web, and cloud technologies, his career has focused primarily on the healthcare industry. He has been awarded as a Microsoft MVP 11 times, most recently as a Windows Dev MVP. Alvin works in the Philadelphia area for Allscripts, a global healthcare software company, as a principal software engineer. He is also a board member of the TechBash Foundation, where he helps organize the annual TechBash developer conference. He has previously worked for companies such as Oracle, Genzeon, CSC, and ITG Pathfinders. Originally from the Allentown, PA area, Alvin currently resides in West Grove, PA with his wife and three daughters.
Read more about Alvin Ashcraft

Right arrow

Chapter 10: Debugging Multithreaded Applications with Visual Studio

Visual Studio 2022 is the latest version of Visual Studio on Mac and Windows. In this chapter, we are going to learn how to leverage the power of Visual Studio when debugging multithreaded .NET applications.

Visual Studio provides several extremely useful tools for developers who need to debug parallel and concurrent .NET applications. This chapter will explore the tools in detail through concrete examples.

In this chapter, we will cover the following topics:

  • Introducing multithreaded debugging
  • Debugging threads and processes
  • Switching and flagging threads
  • Debugging a parallel application

By the end of this chapter, you will have the tools and knowledge you need to debug threading issues in your parallel and concurrent C# code.

Technical requirements

To follow along with the examples in this chapter, the following software is recommended for Windows developers:

  • Visual Studio 2022 version 17.2 or later
  • .NET 6
  • To complete any WinForms or WPF samples, you will need to install the .NET desktop development workload for Visual Studio. These projects will run only on Windows.

All the code examples for this chapter can be found on GitHub at https://github.com/PacktPublishing/Parallel-Programming-and-Concurrency-with-C-sharp-10-and-.NET-6/tree/main/chapter10.

Note

The concepts and tools in this chapter only work with Visual Studio on Windows. If you are building .NET applications on a Mac, the Rider IDE from JetBrains provides several tools for multithreaded debugging – a Threads pane, a Frames view to view frames on a selected thread, and a Parallel Stacks pane. Visual Studio for Mac doesn’t have this kind of support for debugging multithreaded applications yet. You can...

Introducing multithreaded debugging

Debugging is a key component of every .NET developer’s skillset. Nobody ever writes bug-free code and introducing multithreaded constructs to your project only increases the chances of introducing bugs. As .NET and C# have added more support for parallel programming and concurrency, Visual Studio has added more debugging features to support those constructs.

Today, Visual Studio provides the following multithreaded debugging features for the modern .NET developer:

  • Threads: This window shows a list of the threads that are used by your application while debugging. It also indicates which thread is active when it stopped at a breakpoint in your code.
  • Parallel Stacks: This window allows developers to visualize the call stacks for each thread in their application in a single view. Selecting a thread in the window will display call stack information for the selected thread in the Call Stack window.
  • Parallel Watch: This window...

Debugging threads and processes

In this section, we are going to debug BackgroundPingConsoleApp from Chapter 1. You can use your completed project from Chapter 1 or get the project from this chapter’s GitHub repository: https://github.com/PacktPublishing/Parallel-Programming-and-Concurrency-with-C-sharp-10-and-.NET-6/tree/main/chapter10. We will debug the application and discover some of the features of the Debug Location toolbar and the Threads window as we go.

Debugging a project with multiple threads

The project we’ll be working this is a simple one that creates one background thread to check whether the network is available.

Note

The examples in this chapter will be run in the Debug configuration in Visual Studio. When you compile and run a .NET project, you can choose to run a Debug or Release build. While debugging, you will want to select Debug mode so that the project compiles w the symbolic debug information. This is not included in a Release build...

Switching and flagging threads

The Threads window provides so much power when debugging a multithreaded application. We touched on some of these features in the previous section. In this section, we will learn how to switch threads, flag threads, and freeze or thaw a thread. Let’s start by switching between threads in our BackgroundPingConsoleApp project.

Switching threads

You can switch context to a different thread by using the context menu in the Threads window. Run the project and wait for the debugger to pause at the breakpoint in our anonymous method. While the execution is paused in the debugger, right-click on the Main Thread row and select Switch to Thread. The cursor in the debugger should switch positions to the Console.ReadLine() statement. This is where the main thread is waiting for the user to press any key in the console:

Figure 10.6 – Switching threads in the Visual Studio debugger

You can see how this function could be...

Debugging a parallel application

Visual Studio provides several windows for parallel debugging. While the Threads window excels for any type of multithreaded application, other windows provide additional features and views when working with Task objects in our applications.

We will start our tour of these features with the Parallel Stacks window.

Using the Parallel Stacks window

The Parallel Stacks window provides a visual representation of the threads or tasks in the application. These are two distinct views in the window. You can switch between them by selecting Threads or Tasks in the View dropdown box. The following screenshot shows an example of the Threads view while debugging the BackgroundPingConsoleApp project:

Figure 10.10 – Viewing the Parallel Stacks window in the Threads view

The Parallel Stacks window contains a toolbar with the following items from left to right. You can follow along by examining the tooltips for the toolbar items...

Summary

In this chapter, we learned about the Visual Studio features available to multithreaded application developers. We started by working with threads in the Threads window. This is the most universal debugging window when working doing parallel programming in .NET. It can provide essential information, regardless of whether you are working with async tasks, parallel loops, or standard Thread objects.

Next, we learned how to switch, flag, and freeze our threads while debugging. Finally, we looked at some of the advanced debugging tools for developers who are using Task objects or async/await in their code. The Parallel Stacks and Parallel Watch windows take task debugging to the next level. Finally, we took a quick look at the Tasks window and the Debug Location toolbar.

In the next chapter, Chapter 11, we will dive deeper into the different methods available to cancel concurrent and parallel work with .NET.

Questions

  1. How can you debug multiple processes in Visual Studio?
  2. What is the default grouping of threads in the Threads window?
  3. How can you add more columns to the Tasks or Threads window?
  4. Which debug window displays a visual representation of the current threads or tasks?
  5. What file format can you export from the Parallel Stacks window?
  6. How many Parallel Watch windows can you open?
  7. Which Visual Studio toolbar provides information about the processes and threads you are currently debugging?
  8. How can you filter the Threads window to only show the threads that have been created for your code?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Parallel Programming and Concurrency with C# 10 and .NET 6
Published in: Aug 2022Publisher: PacktISBN-13: 9781803243672
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Alvin Ashcraft

Alvin Ashcraft is a software engineer and developer community champion with over 25 years of experience in software development. Working primarily with Microsoft Windows, web, and cloud technologies, his career has focused primarily on the healthcare industry. He has been awarded as a Microsoft MVP 11 times, most recently as a Windows Dev MVP. Alvin works in the Philadelphia area for Allscripts, a global healthcare software company, as a principal software engineer. He is also a board member of the TechBash Foundation, where he helps organize the annual TechBash developer conference. He has previously worked for companies such as Oracle, Genzeon, CSC, and ITG Pathfinders. Originally from the Allentown, PA area, Alvin currently resides in West Grove, PA with his wife and three daughters.
Read more about Alvin Ashcraft