/async-workshop-2023

Slides, code samples, and hands-on labs for the "Understanding Asynchronous Programming in C#" workshop (2022)

Primary LanguageC#MIT LicenseMIT

Understanding Asynchronous Programming in C# [2022 Workshop]

This project contains slides and code samples for the "Understanding Asynchronous Programminc in C#" workshop with Jeremy Clark.

Overview and Objectives

Level: Introductory / Intermediate
Asynchronous code is everywhere. In our C# code, we "await" method calls to services and databases; and more and more packages that we use every day have asynchronous methods. But do you really understand what this does?

Understanding is critical. When done correctly, we can make our applications more responsive, faster, and reliable. When done incorrectly, we can block threads or even hang the application entirely.

In this half-day workshop, we'll start at the beginning to see how "await" relates to "Task”. We'll do this by calling an asynchronous method, getting a result, and handling errors that come up. We will create our own "awaitable" methods to see how Task and return types work together. With our own methods, we'll also better understand why we may (or may not) care about getting back to the original calling thread. We'll also cover some dangers, such as using "async void" or misusing ".Result". Finally, we'll use Task to run multiple operations in parallel to make things faster. With all of these skills, we can write more effective asynchronous code.

You will learn:

  • How to use "await" and "Task" to run asynchronous methods
  • About handling errors from asynchronous processes
  • About writing your own asynchronous methods How to avoid pitfalls such as "async void" and ".Result"
  • About running multiple methods in parallel

Pre-Requisites
For this workshop, it is assumed that you have some experience with C#, but no specific asynchronous programming experience is needed. To run the sample code, you will need the NET 6.0 SDK installed. Jeremy will be using Visual Studio 2022 (Community Edition), but the code samples will run using Visual Studio Code or the editor of your choice.

Links:

Running the Samples

The sample code uses .NET 6.0. The console and web samples will run on all Window, macOS, and Linux versions that support .NET 6.0. The desktop samples are Windows-only.

Samples have been tested with Visual Studio 2022 and Visual Studio Code.

All samples require the "Person.Service" web service be running. To start the service, navigate to the "Person.Service" folder from the command line and type "dotnet run".

Ex:

C:\understanding-async\People.Service> dotnet run

The service can be found at http://localhost:9874/people

Project Layout

The "DemoCode" folder contains the code samples used in the workshop.
Shared Projects

  • People.Service
    A web service that supplies data for the sample projects.
  • TaskAwait.Shared
    A library with data types that are shared across projects (primarily the "Person" type).
  • TaskAwait.Library
    A library with asynchronous methods that access the web service. These methods are called in the various applications detailed below.
    Relevant file: PersonReader.cs

Concurrent Samples
The Concurrent samples run asynchronous methods, get results, handle exceptions, and support cancellation (unless otherwise noted).

  • Concurrent.UI.Console
    A console application (Windows, macOS, Linux)
    Relevant file: Program.cs
  • Concurrent.UI.Desktop
    A WPF desktop application (Windows only).
    Relevant file: MainWindow.xaml.cs
  • Concurrent.UI. Web
    A web application (Windows, macOS, Linux).
    Note: this application does not support cancellation.
    Relevant file: Controllers/PeopleController.cs

Parallel Samples
The Parallel samples use Task to run asynchronous methods in parallel - also get results, handle exceptions, and support cancellation (unless otherwise noted).

  • Parallel.Basic
    A console application that does not support cancellation or error handling (Windows, macOS, Linux).
    Relevant file: Program.cs
  • Parallel.UI.Console
    A console application (Windows, macOS, Linux).
    Relevant file: Program.cs
  • Parallel.UI.Desktop
    A WPF desktop application (Windows only).
    Relevant file: MainWindow.xaml.cs
  • Parallel.UI. Web
    A web application (Windows, macOS, Linux).
    Note: this application does not support cancellation.
    Relevant file: Controllers/PeopleController.cs

Progress Reporting (Bonus Material)
The Progress Reporting samples show how to report progress from an asynchronous method - in this case, as a percentage complete. These also get results, handle exceptions, and support cancellation.

  • ProgressReport.UI.Console
    A console application that reports percentage complete progress through text. Ex: "21% Complete". (Windows, macOS, Linux)
    Relevant file: Program.cs
  • Parallel.UI.Desktop
    A WPF desktop application that reports percentage complete progress through a graphical progress bar. (Windows only)
    Relevant file: MainWindow.xaml.cs
  • TaskAwait.Library
    This shared library contains a method that supports progress reporting.
    Relevant method:
public async Task<List<Person>> GetPeopleAsync(IProgress<int> progress,
    CancellationToken cancelToken = new CancellationToken()) {...}

Hands-On Labs (Bonus Material)

The "Labs" folder contains hands-on labs. The labs are integrated throughout the workshop day.

Each lab consists of the following:

  • Labxx-Instructions (Markdown)
    A markdown file containing the lab instructions. This includes the scenario, a set of goals, and step-by-step instructions.
    This can be viewed on GitHub or in Visual Studio Code (just click the "Open Preview to the Side" button in the upper right corner).

  • Starter (Folder)
    This folder contains the starting code for the lab.

  • Completed (Folder)
    This folder contains the completed solution. If at any time, you get stuck during the lab, you can check this folder for a solution.

Additional Resources

Related Articles (by Jeremy)

Video Series & Articles (by Jeremy)
Each of these has a lot of supporting links:

Other Resources
Stephen Cleary has lots of great articles, books, and practical advice.

Stephen Toub has great articles, too (generally with advanced insights).

Articles / Videos Suggested by prior Workshop Attendees

For more information, visit jeremybytes.com.