CKTIS / C#
System.Threading.SemaphoreSlim: практическа справка
System.Threading.SemaphoreSlim: практическа справка: SemaphoreSlim ограничава броя едновременни влизания и поддържа WaitAsync.
Версия: .NET API; verified against .NET 10 reference surface
Синтаксис
System.Threading.SemaphoreSlim /* select a documented member or constructor */SemaphoreSlim ограничава броя едновременни влизания и поддържа WaitAsync.
Използвайте за ограничаване на async операции, като освобождавате във finally.
Всеки успешен Wait изисква един Release; излишен Release може да хвърли SemaphoreFullException.
Отменен WaitAsync хвърля OperationCanceledException; Dispose при активни чакащи изисква координация.
Пълната програма действително използва System.Threading.SemaphoreSlim; точният очакван stdout е 0.
Примери
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();
Резултат
0
Пълната програма действително използва `System.Threading.SemaphoreSlim`; точният очакван stdout е `0`.