{
  "id": 12188101,
  "title": "Sequential async execution in .NET: SemaphoreSlim, Channels, and TaskFlow",
  "url": "https://urgent.news/2026/10/05/sequential-async-execution-in-net-semaphoreslim-channels-and-taskflow",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-05T16:31:33.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/dombrovsky/sequential-async-execution-in-net-semaphoreslim-channels-and-taskflow-5blb"
  },
  "original_language": "en",
  "account": "The provided example demonstrates a pattern for managing concurrent access to a non-thread-safe resource in .NET. The `InstrumentController` class owns an `IInstrumentSession` object, which is not thread-safe. To ensure exclusive access to the session, the class uses a `SemaphoreSlim` with a count of 1, allowing only one thread to access the session at a time.\n\nThe `InstrumentController` class exposes methods for setting the range, reading, and calibrating the instrument asynchronously. Each method acquires the semaphore before performing the corresponding operation and releases it afterward. This ensures that the session is not accessed concurrently by multiple threads, preventing potential data races or inconsistent states.\n\nThe disposal contract of the `InstrumentController` class is crucial. The `Dispose` method must be called only once, and it must wait for all ongoing asynchronous operations to complete before releasing the resources. This is achieved by marking the object as disposed and disposing of the `IInstrumentSession` and `SemaphoreSlim` in a finally block, ensuring that the disposal process is not concurrent with other members.\n\nThe example showcases a simple yet effective way to manage concurrent access to a non-thread-safe resource using a `SemaphoreSlim`. It demonstrates the importance of proper resource management and synchronization in concurrent programming scenarios.",
  "summary": "Familiar pattern? using System ; using System.Threading ; using System.Threading.Tasks ; public sealed class InstrumentController : IDisposable { private readonly IInstrumentSession _session ; private readonly SemaphoreSlim _gate = new ( 1 , 1 ); private bool _disposed ; // Takes exclusive ownership of a session that is not thread-safe. public InstrumentController ( IInstrumentSession session )…",
  "key_points": [
    "InstrumentController class manages IInstrumentSession using SemaphoreSlim",
    "Ensures exclusive access to non-thread-safe resource with semaphore count of 1",
    "Dispose method waits for async operations before releasing resources"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}