Skip to content

Code samples, slides, and links for "Better Parallel Code with C# Channels"

License

Notifications You must be signed in to change notification settings

jeremybytes/csharp-channels-presentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Better Parallel Code with C# Channels

Abstract

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)

Project Layout

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.

Development Environments

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.

Running the Service

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:

[{"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}, 
{...}]
[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 Console Application

The /people-demo/PeopleViewer folder contains a console application that uses channels. The relevant code is in the "Program.cs" file.

Additional Sample

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

Resources

Additional Repositories

About

Code samples, slides, and links for "Better Parallel Code with C# Channels"

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages