/// <summary> /// Throws the specified <paramref name="exception"/> on the oldest wait matching the specified <paramref name="key"/>. /// </summary> /// <param name="key">The unique WaitKey for the wait.</param> /// <param name="exception">The Exception to throw.</param> public void Throw(WaitKey key, Exception exception) { if (Waits.TryGetValue(key, out var queue) && queue.TryDequeue(out var wait)) { wait.TaskCompletionSource.SetException(exception); } }
/// <summary> /// Adds a new wait for the specified <paramref name="key"/> and with the specified <paramref name="timeout"/>. /// </summary> /// <typeparam name="T">The wait result type.</typeparam> /// <param name="key">A unique WaitKey for the wait.</param> /// <param name="timeout">The wait timeout.</param> /// <param name="cancellationToken">The cancellation token for the wait.</param> /// <returns>A Task representing the wait.</returns> public Task <T> Wait <T>(WaitKey key, int?timeout = null, CancellationToken?cancellationToken = null) { timeout = timeout ?? DefaultTimeout; var wait = new PendingWait() { TaskCompletionSource = new TaskCompletionSource <T>(TaskCreationOptions.RunContinuationsAsynchronously), DateTime = DateTime.UtcNow, TimeoutAfter = (int)timeout, CancellationToken = cancellationToken, }; Waits.AddOrUpdate(key, new ConcurrentQueue <PendingWait>(new[] { wait }), (_, queue) => { queue.Enqueue(wait); return(queue); }); return(((TaskCompletionSource <T>)wait.TaskCompletionSource).Task); }
/// <summary> /// Adds a new wait for the specified <paramref name="key"/> which does not time out. /// </summary> /// <typeparam name="T">The wait result type.</typeparam> /// <param name="key">A unique WaitKey for the wait.</param> /// <param name="cancellationToken">The cancellation token for the wait.</param> /// <returns>A Task representing the wait.</returns> public Task <T> WaitIndefinitely <T>(WaitKey key, CancellationToken?cancellationToken = null) { return(Wait <T>(key, MaxTimeoutValue, cancellationToken)); }
/// <summary> /// Adds a new wait for the specified <paramref name="key"/> which does not time out. /// </summary> /// <param name="key">A unique WaitKey for the wait.</param> /// <param name="cancellationToken">The cancellation token for the wait.</param> /// <returns>A Task representing the wait.</returns> public Task WaitIndefinitely(WaitKey key, CancellationToken?cancellationToken = null) { return(WaitIndefinitely <object>(key, cancellationToken)); }
/// <summary> /// Adds a new wait for the specified <paramref name="key"/> and with the specified <paramref name="timeout"/>. /// </summary> /// <param name="key">A unique WaitKey for the wait.</param> /// <param name="timeout">The wait timeout.</param> /// <param name="cancellationToken">The cancellation token for the wait.</param> /// <returns>A Task representing the wait.</returns> public Task Wait(WaitKey key, int?timeout = null, CancellationToken?cancellationToken = null) { return(Wait <object>(key, timeout, cancellationToken)); }
/// <summary> /// Completes the oldest wait matching the specified <paramref name="key"/>. /// </summary> /// <param name="key">The unique WaitKey for the wait.</param> public void Complete(WaitKey key) { Complete <object>(key, null); }