/// <summary> /// Disposes of the instance. /// </summary> public void Dispose() { lock (sync) { if (disposed) { return; } if (attempt != null) { attempt.Dispose(); attempt = null; } disposed = true; } }
/// <summary> /// Returns a task that completes when the job is complete. /// </summary> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <TResult> WaitAsync(CancellationToken cancellationToken = default) { lock (sync) { if (disposed) { throw new ObjectDisposedException(nameof(AsyncJob <TResult>)); } // entire job was canceled cancellationToken.ThrowIfCancellationRequested(); // initialize attempt if (attempt == null) { attempt = new AsyncJobAttempt(Work, Exit); } // subscribe new waiter return(attempt.WaitAsync(cancellationToken)); } }
/// <summary> /// Invoked when the attempt exits early. /// </summary> void Exit() { lock (sync) attempt = null; }