Skip to content

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

Notifications You must be signed in to change notification settings

jeremybytes/async-workshop-2024

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asynchronous and Parallel Programming in C#

Full-day Workshop

To download just the labs, you can visit this repository:
https://github.com/jeremybytes/async-workshop-labs-only-2024

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

Overview and Objectives

Level: Intermediate

Asynchronous programming is a critical skill to take full advantage of today's multi-core systems. But async programming brings its own set of issues. In this workshop, we'll work through some of those issues and get comfortable using parts of the .NET Task Parallel Library (TPL).

We'll start by calling asynchronous methods using the Task Asynchronous Pattern (TAP), including how to handle exceptions and cancellation. With this in hand, we'll look at creating our own asynchronous methods and methods that use asynchronous libraries. Along the way, we'll see how to avoid deadlocks, how to isolate our code for easier async, and why it's important to stay away from "async void".

In addition, we'll look at some patterns for running code in parallel, including using Parallel.ForEachAsync, channels, and other techniques. We'll see pros and cons so that we can use the right pattern for a particular problem.

Throughout the day, we'll go hands-on with lab exercises to put these skills into practice.

Objectives:

  • Use asynchronous methods with Task and await
  • Create asynchronous methods and libraries
  • How to avoid deadlocks and other pitfalls
  • Understand different parallel programming techniques

Pre-Requisites

Basic understanding of C# and object-oriented programming (classes, inheritance, methods, and properties). No prior experience with asynchronous programming is necessary; we'll take care of that as we go.

Attendee Requirements:

  • You must provide your own laptop computer (Windows or Mac) for this hands-on lab.

  • You need to have the .NET 6 SDK or .NET 8 SDK installed as well as the code editor of your choice (Visual Studio 2022 Community Edition or Visual Studio Code are both good (free) choices).

  • Interactive labs, web application samples, and console samples will work with Windows, macOS, and Linux (anywhere .NET 6/8 will run).

  • WPF desktop samples will only work on Windows machines. There are equivalent web and console examples for these projects.

Links:

Running the Samples

The sample code use .NET 6 or .NET 8. The console and web samples will run on all Window, macOS, and Linux versions that support .NET. 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

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:

BackgroundWorker Component

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published