Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

System.Threading.SemaphoreSlim: odniesienie praktyczne

System.Threading.SemaphoreSlim: odniesienie praktyczne: SemaphoreSlim ogranicza liczbę współbieżnych wejść i obsługuje WaitAsync.

Wersja: .NET API; verified against .NET 10 reference surface

Składnia

System.Threading.SemaphoreSlim /* select a documented member or constructor */

SemaphoreSlim ogranicza liczbę współbieżnych wejść i obsługuje WaitAsync.

Stosuj do throttlingu operacji async, zwalniając w finally.

Każde udane Wait wymaga jednego Release; nadmiarowy Release może zgłosić SemaphoreFullException.

WaitAsync z anulowaniem rzuca OperationCanceledException; Dispose przy aktywnych waiterach wymaga koordynacji.

Kompletny program rzeczywiście używa System.Threading.SemaphoreSlim; oczekiwany stdout to dokładnie 0.

Przykłady

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

using var semaphore = new SemaphoreSlim(1, 1);
await semaphore.WaitAsync();
Console.WriteLine(semaphore.CurrentCount);
semaphore.Release();

Wynik

0

Kompletny program rzeczywiście używa `System.Threading.SemaphoreSlim`; oczekiwany stdout to dokładnie `0`.

Źródła