Skip to main content
added 19 characters in body
Source Link
dfhwze
  • 14.2k
  • 3
  • 40
  • 101

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;
  }

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 (WaitHandle.WaitTimeout 
         == WaitHandle.WaitAny(new[] { _semaphore, ct.WaitHandle }, timeout))
     {
         return await Task.FromResult(false);
     }
     ct.ThrowIfCancellationRequested();
     return await Task.FromResult(true);
 }

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)
  {
      var success = await Task.Run(() =>
      {
          return WaitHandle.WaitTimeout
              != WaitHandle.WaitAny(new[] { _semaphore, ct.WaitHandle }, timeout);
      });
      ct.ThrowIfCancellationRequested();
      return success;
  }
Post Undeleted by dfhwze
Post Deleted by dfhwze
Source Link
dfhwze
  • 14.2k
  • 3
  • 40
  • 101

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 (WaitHandle.WaitTimeout 
         == WaitHandle.WaitAny(new[] { _semaphore, ct.WaitHandle }, timeout))
     {
         return await Task.FromResult(false);
     }
     ct.ThrowIfCancellationRequested();
     return await Task.FromResult(true);
 }