Perhaps you could rewrite the wait operations to await both the semaphore or cancellation token.
public async Task<bool> WaitOne(TimeSpan timeout, CancellationToken ct) { DateTime start = DateTime.UtcNow; while (!_semaphore.WaitOne(0)) { ct.ThrowIfCancellationRequested(); if (DateTime.UtcNow < start.Add(timeout)) return false; await Task.Delay(100, ct); } return true; }
public async Task<bool> WaitOne(TimeSpan timeout, CancellationToken ct)
{
if (WaitHandlevar success = await Task.WaitTimeoutRun(() =>
{
== WaitHandle.WaitAny(new[] { _semaphore, ct.WaitHandle }, timeout))
return WaitHandle.WaitTimeout
{
return await!= TaskWaitHandle.FromResultWaitAny(falsenew[] { _semaphore, ct.WaitHandle }, timeout);
});
ct.ThrowIfCancellationRequested();
return awaitreturn Task.FromResult(true);success;
}