Skip to content

Semaphore and SemaphoreSlim sample: .NET 11, release in finally, caller timing - #2392

Merged
vladimir-pecanac-main merged 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/117092-semaphore-introduction-csharp
Nov 16, 2026
Merged

vladimir-pecanac-main merged 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/117092-semaphore-introduction-csharp

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Moves both projects of threads-csharp/IntroductionToSemaphoreInCsharp to net11.0 with MSTest 4.5.3, Microsoft.NET.Test.Sdk 18.12.0 and coverlet.collector 10.1.0. Both workers now release the semaphore in a finally block, the null check on the non-nullable ProcessParams is gone, ReleaseMultipleTimes disposes its Semaphore, and Program.cs times how long each Access...Async call keeps the caller busy and prints the SemaphoreFullException instead of ending on it. The tests replace ExpectedException (gone in MSTest 4) with Assert.ThrowsExactly, assert the caller-busy bounds next to the thread count with a 200 ms test delay, and add a test for over-release on a SemaphoreSlim created without maxCount.

Both projects target net11.0. MSTest 4.5.3, Microsoft.NET.Test.Sdk 18.12.0 and coverlet.collector 10.1.0. Both workers release the semaphore in a finally block, the SemaphoreSlim null check is removed, ReleaseMultipleTimes disposes its Semaphore, Program.cs times how long each call keeps the caller busy and prints the SemaphoreFullException. Tests use Assert.ThrowsExactly, assert the caller-busy bounds and cover over-release on a SemaphoreSlim without maxCount.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 314d46d into CodeMazeBlog:main Nov 16, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant