Created
April 6, 2025 22:08
-
-
Save AnthonyGiretti/bf23121f8dde5a021920859c73ab69ed to your computer and use it in GitHub Desktop.
C# 13 EnterScope usage example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
class LockingDemo | |
{ | |
private static System.Threading.Lock lock = new(); | |
static void Main() | |
{ | |
PerformSafeOperation(); | |
} | |
static void PerformSafeOperation() | |
{ | |
// Enter a thread-safe region using the new scoped lock | |
using var lockScope = lock.EnterScope(); | |
Console.WriteLine("Executing operation in a synchronized context..."); | |
Thread.Sleep(500); // Simulate processing | |
Console.WriteLine("Operation complete. Exiting critical section."); | |
// Lock is released automatically when scope ends | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment