using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace EVCB_OCPP.WSServer.Helper; public class SemaphoreWrapper : IDisposable { public async static Task WaitAsync(SemaphoreSlim semaphore) { var toReturn = new SemaphoreWrapper(semaphore); await semaphore.WaitAsync(); toReturn.isLockAquired = true; return toReturn; } public async static Task TryGet(SemaphoreSlim semaphore) { var toReturn = new SemaphoreWrapper(semaphore); if (semaphore.Wait(0)) { toReturn.isLockAquired = true; return toReturn; } return null; } private SemaphoreWrapper(SemaphoreSlim semaphore) { this.semaphore = semaphore; } public void Dispose() { if (isLockAquired) { semaphore.Release(); } } private readonly SemaphoreSlim semaphore; private bool isLockAquired = false; }