/// <summary> /// Returns a task that will receive the last value or the exception produced by the observable sequence. /// </summary> /// <typeparam name="TResult">The type of the elements in the source sequence.</typeparam> /// <param name="observable">Observable sequence to convert to a task.</param> /// <param name="cancellationToken">Cancellation token that can be used to cancel the task, causing unsubscription from the observable sequence.</param> /// <param name="state">The state to use as the underlying task's AsyncState.</param> /// <returns>A task that will receive the last element or the exception produced by the observable sequence.</returns> /// <exception cref="ArgumentNullException"><paramref name="observable"/> is null.</exception> public static Task <TResult> ToTask <TResult>(this IObservable <TResult> observable, CancellationToken cancellationToken, object state) { if (observable == null) { throw new ArgumentNullException("observable"); } var hasValue = false; var lastValue = default(TResult); var tcs = new TaskCompletionSource <TResult>(state); var disposable = new SingleAssignmentDisposable(); var ctr = default(CancellationTokenRegistration); if (cancellationToken.CanBeCanceled) { ctr = cancellationToken.Register(() => { disposable.Dispose(); tcs.TrySetCanceled(cancellationToken); }); } var taskCompletionObserver = Observer.Create <TResult>( value => { hasValue = true; lastValue = value; }, ex => { tcs.TrySetException(ex); ctr.Dispose(); // no null-check needed (struct) disposable.Dispose(); }, () => { if (hasValue) { tcs.TrySetResult(lastValue); } else { tcs.TrySetException(new InvalidOperationException("Strings_Linq.NO_ELEMENTS")); } ctr.Dispose(); // no null-check needed (struct) disposable.Dispose(); } ); // // Subtle race condition: if the source completes before we reach the line below, the SingleAssigmentDisposable // will already have been disposed. Upon assignment, the disposable resource being set will be disposed on the // spot, which may throw an exception. (Similar to TFS 487142) // try { // // [OK] Use of unsafe Subscribe: we're catching the exception here to set the TaskCompletionSource. // // Notice we could use a safe subscription to route errors through OnError, but we still need the // exception handling logic here for the reason explained above. We cannot afford to throw here // and as a result never set the TaskCompletionSource, so we tunnel everything through here. // disposable.Disposable = observable.Subscribe/*Unsafe*/ (taskCompletionObserver); } catch (Exception ex) { tcs.TrySetException(ex); } return(tcs.Task); }