April 23, 2024


Develop Technology For The Connected World

How to use structured concurrency in C#

6 min read

Fashionable programming languages these types of as C# facilitate the successful use of sources by enabling numerous operations to be executed concurrently. Here concurrently often signifies asynchronously, when a number of processes must share the very same CPU core. Having said that, taking care of concurrent execution paths (making use of threads or tasks) can swiftly develop into difficult owing to the overhead of juggling asynchronous responsibilities.

“Structured concurrency” is a programming paradigm that was launched to handle this. Structured concurrency promotes a additional disciplined and organized way to manage concurrency. In this article, we will delve into structured concurrency and see how it can be applied in C#. 

Generate a console software challenge in Visible Studio

1st off, let’s build a .Web Main console application venture in Visible Studio. Assuming Visual Studio 2022 is mounted in your program, observe the actions outlined below to make a new .Net Main console application venture.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new venture.”
  3. In the “Create new project” window, pick out “Console App (.Web Core)” from the checklist of templates displayed.
  4. Click Following.
  5. In the “Configure your new project” window revealed following, specify the identify and locale for the new challenge.
  6. Simply click Upcoming.
  7. In the “Additional information” window, pick out “.Net 7. (Normal Term Aid)” as the Framework version you would like to use.
  8. Simply click Create.

We’ll use this .Internet 7 console software undertaking to get the job done with structured concurrency in the subsequent sections of this report.

What is asynchronous programming?

Asynchronous programming enables applications to execute source-intense operations concurrently, without having having to block on the principal or the executing thread of the software. Classic approachеs to async programming, likе working with callback capabilities or thе Job Parallеl Library (TPL), oftеn drop brief whеn it comеs to taking care of concurrеncy and managing thе lifеtimе of asynchronous opеrations. Structurеd concurrеncy offеrs a morе structurеd and intuitive way of handling asynchronous programming.

What is structured concurrency?

Structured concurrency is a technique for dealing with concurrent functions in asynchronous programming. It relies on endeavor scopes and proper useful resource cleanup to supply several rewards including cleaner code, more simple error handling, and avoidance of source leaks. Structured concurrency emphasizes the plan that all asynchronous tasks really should be structured within just a particular context, making it possible for developers to properly compose and handle the movement of these duties.

To far better regulate the execution of async functions, structured concurrency introduces the notion of activity scopes. Process scopes supply a reasonable unit that sets boundaries for concurrent duties. All jobs executed within just a job scope are carefully monitored and their lifecycle is cautiously managed. If any endeavor in just the scope encounters failure or cancellation, all other jobs within that scope are mechanically canceled as nicely. This makes certain appropriate cleanup and prevents re­source leaks.

Added benefits of structured concurrency

Below are some of the crucial benefits of structured concurrency:

  • Cleaner code: Structured concurrency assists you create clean and maintainable code by preserving asynchronous functions logically organized within defined scopes. With structured concurrency, duties have a crystal clear scope. When the scope exits, we can be confident that all responsibilities in that scope were being concluded.
  • Economical resource cleanup: Structured concurrency supplies an computerized resource cleanup mechanism. It makes sure that all tasks within just a scope are canceled if any activity encounters an mistake or is canceled, effectively managing the cleanup of resources.
  • Superior error handling: With structured concurrency, managing problems and protecting against mistake propagation are designed a lot easier by canceling all responsibilities within a scope. This encourages much better manage circulation and lessens complexity.
  • Eliminates useful resource leaks: In traditional ways to asynchronous programming, there is a danger of resource leaks when an operation fails or will get canceled. Structured concurrency aids address this trouble by ensuring that accurate cleanup is enforced.

Structured concurrency illustration in C#

In C#, we can implement structured concurrency by working with the attributes readily available in the Technique.Threading.Responsibilities.Channels namespace. This namespace features handy constructs like Channel and ChannelReader that make utilizing structured concurrency easier.

Acquire a look at the subsequent instance code, which I will describe beneath.

making use of Procedure
employing Process.Threading.Channels
employing Technique.Threading.Tasks
async Undertaking MyAsyncOperation (ChannelWriter channelWrite­r)

    await channelWriter.WriteAsync(100)

async Activity Key()

    var channel = Channel.CreateUnbounde­d()
    var channelWriter = channel.Author
    await making use of (channel.Reader.EnterAsync())
        var taskA = MyAsyncOperation (channelWriter)
        var taskB = MyAsyncOperation (channelWriter)
        await Undertaking.WhenAll(taskA, taskB)
    await channel.Reader.Completion

In the case in point code earlier mentioned, we establish a channel to help conversation concerning the most important thread and concurrent tasks. The MyAsyncOperation strategy signifies the asynchronous operation that will be executed concurrently. By using the EnterAsync strategy of the ChannelReader object, we enter a endeavor scope. Right here various duties are scheduled and waited employing a simply call to the the Task.WhenAll system. If any activity fails or is canceled, then all other jobs in that scope also will be canceled. And lastly, the Writer instance is closed and we await the completion of the Reader.

Put in the Nito.StructuredConcurrency NuGet offer

You can implement structured concurrency using the Nito.StructuredConcurrency NuGet package. Ahead of we dive into utilizing this offer, let us set up it into the console application task we made before. To do this, choose the job in the Answer Explorer window and suitable-simply click and pick out “Manage NuGet Packages.” In the NuGet Deal Supervisor window, research for the Nito.StructuredConcurrency bundle and put in it.

Alternatively, you can set up the Nito.StructuredConcurrency bundle through the NuGet Offer Manager console by moving into the line of code under.

PM> Install-Package Nito.StructuredConcurrency

Use the TaskGroup course to build undertaking scopes

You can generate a job scope in which you can define the do the job to be carried out by your asynchronous responsibilities. To do this, you can use the RunGroupAsync technique of the TaskGroup class as shown in the code snippet supplied beneath.

var group = TaskGroup.RunGroupAsync(default, group =>

    group.Run(async token => await Process.Delay(TimeSpan.FromSeconds(1), token))
    group.Operate(async token => await Undertaking.Hold off(TimeSpan.FromSeconds(2), token))
    team.Operate(async token => await Activity.Hold off(TimeSpan.FromSeconds(3), token))
await team

In the preceeding code snippet, notice how the delegate is passed to the RunGroupAsync method. This delegate signifies the first get the job done item. The subsequent work merchandise are included to the very same activity group. When all the do the job things have done their execution, the control exits the activity team and the team is shut.

In the celebration of an exception, the job team enters a faulted state.

var group = TaskGroup.RunGroupAsync(default, team =>

    team.Run(async token =>
        await Task.Delay(TimeSpan.FromSeconds(1), token)
        throw new Exception("This is a test exception.")
await team

Activity teams ignore jobs that have been cancelled, i.e., they capture and disregard any occurrences of OperationCanceledException. Additionally, process teams can possess assets, which are disposed when the execution of the activity team is entire.

var groupTask = TaskGroup.RunGroupAsync(default, async team =>

    await team.AddResourceAsync(myResource)
    group.Run(async token => await myResource.PerformWork(token))
await groupTask

Structurеd concurrеncy offеrs a disciplinеd method to handling concurrеnt responsibilities by еnsuring that duties havе a clеar scopе and lifеtimе. It presents considerable gains in conditions of code maintainability, mistake handling, and source administration in asynchronous programming situations. By pursuing this strategy, dеvеlopеrs can rеducе thе risk of prevalent pitfalls associatеd with concurrеncy, these as dangling responsibilities or unhandlеd еxcеptions, lеading to morе maintainablе and rеliablе codе.

Copyright © 2023 IDG Communications, Inc.

Copyright © All rights reserved. | Newsphere by AF themes.