Skip to content

Instantly share code, notes, and snippets.

@AnthonyGiretti
Created April 6, 2025 22:08
Show Gist options
  • Save AnthonyGiretti/bf23121f8dde5a021920859c73ab69ed to your computer and use it in GitHub Desktop.
Save AnthonyGiretti/bf23121f8dde5a021920859c73ab69ed to your computer and use it in GitHub Desktop.
C# 13 EnterScope usage example
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