1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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<SemaphoreWrapper> WaitAsync(SemaphoreSlim semaphore)
- {
- var toReturn = new SemaphoreWrapper(semaphore);
- await semaphore.WaitAsync();
- toReturn.isLockAquired = true;
- return toReturn;
- }
- public async static Task<SemaphoreWrapper> 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;
- }
|