Producer/consumer problems show up in a lot of programming scenarios, including data processing and machine learning. Channels were added to .NET Core 3.0 and give us a thread-safe way to communicate between producers and consumers, and we can run them all concurrently. In this presentation, we will explore channels by comparing parallel tasks with continuations to using a producer/consumer model. In the end, we'll have another tool in our toolbox to help us with concurrent programming.
Recorded Presentation: Using Channels in C# to Enhance Concurrent Code - Dot Net North (May 2021)
To build and run the code, you will need to have .NET 8 installed on your machine. The demo project will run wherever .NET 8 will run (Windows, macOS, Linux).
/people-demo/PeopleViewer contains a console application that uses channels program
/people-demo/People.Service contains a service (used by the console application)
The "PeopleViewer" program is a console application that calls the service and displays the output. In order to show concurrency, the application gets each record individually.
Visual Studio 2022
The "ChannelsDemo.sln" contains both of the projects listed above. The solution is set to automatically start both the service and the console application. So, hit "F5" to start the application & service.
Visual Studio Code
In Visual Studio Code, you will want to start the service separately from the command line (see "Running the Service"). You can leave the service running while working with the console application.
The .NET service can be started from the command line by navigating to the ".../people-demo/People.Service" directory and typing dotnet run
. This provides endpoints at the following locations:
- http://localhost:9874/people
Provides a list of "Person" objects. This service will delay for 3 seconds before responding. Sample result:
[{"id":1,"givenName":"John","familyName":"Koenig","startDate":"1975-10-17T00:00:00-07:00","rating":6,"formatString":null},
{"id":2,"givenName":"Dylan","familyName":"Hunt","startDate":"2000-10-02T00:00:00-07:00","rating":8,"formatString":null},
{...}]
- http://localhost:9874/people/ids
Provides a list of "id" values for the collection. Sample:
[1,2,3,4,5,6,7,8,9]
- http://localhost:9874/people/1
Provides an individual "Person" record based on the "id" value. This service will delay for 1 second before responding. Sample record:
{"id":1,"givenName":"John","familyName":"Koenig","startDate":"1975-10-17T00:00:00-07:00","rating":6,"formatString":null}
The /people-demo/PeopleViewer folder contains a console application that uses channels. The relevant code is in the "Program.cs" file.
The "digit-display" folder contains an additional code sample. This application is a naive machine learning project that uses concurrent operations and channels. The version included here has a Windows-only user interface.
For more additional information on this project (as well as a set of console applications that run cross-platform), visit the the main repository for the "digit-display" project:
Github: jeremybytes/digit-display
- Recorded Presentation: Using Channels in C# to Enhance Concurrent Code - Dot Net North (May 2021)
- An Introduction to Channels in C# - Jeremy Clark
- What's the Difference between Channel and ConcurrentQueue in C# - Jeremy Clark
- An Introduction to System.Threading.Channels - Stephen Toub
- Channel<T> Class - Microsoft Docs
- Channel Exceptions
Samples that explore exception handling for producers and consumers in parallel code. - I'll Get Back to You: Task, Await, and Asynchronous Methods in C#
Presentation resources for understanding Task & await. Includes slides, code, videos, and articles - Understanding Asynchronous Programming in C# - Virtual Training
Virtual training resources for understanding Task, await, and parallel programming. Includes slides, code samples, and articles - Workshop: Asynchronous and Parallel Programming in C#
Workshop resources for understanding Task, await, and parallel programming (including self-guided, hands-on labs). Includes slides, code samples, labs, and articles