protected static TAsyncResult End <TAsyncResult>(IAsyncResult result) where TAsyncResult : AsyncResult { if (result == null) { throw Fx.Exception.ArgumentNull("result"); } TAsyncResult asyncResult = result as TAsyncResult; if (asyncResult == null) { throw Fx.Exception.Argument("result", CommonResources.InvalidAsyncResult); } if (asyncResult.endCalled) { throw Fx.Exception.AsError(new InvalidOperationException(CommonResources.AsyncResultAlreadyEnded)); } asyncResult.endCalled = true; if (!asyncResult.isCompleted) { lock (asyncResult.ThisLock) { if (!asyncResult.isCompleted && asyncResult.manualResetEvent == null) { asyncResult.manualResetEvent = new ManualResetEvent(asyncResult.isCompleted); } } } if (asyncResult.manualResetEvent != null) { asyncResult.manualResetEvent.WaitOne(); #if NET451 asyncResult.manualResetEvent.Close(); #else asyncResult.manualResetEvent.Dispose(); #endif } if (asyncResult.exception != null) { // Trace before PrepareForRethrow to avoid weird callstack strings #if NET451 Fx.Exception.TraceException(asyncResult.exception, asyncResult.TraceEventType, asyncResult.Activity); #else Fx.Exception.TraceException(asyncResult.exception, TraceEventType.Verbose, asyncResult.Activity); #endif ExceptionDispatcher.Throw(asyncResult.exception); } return(asyncResult); }
public static void EndAsyncResult(IAsyncResult asyncResult) { Task task = asyncResult as Task; if (task == null) { throw Fx.Exception.AsError(new ArgumentException(CommonResources.InvalidAsyncResult)); } try { task.Wait(); } catch (AggregateException ae) { ExceptionDispatcher.Throw(ae.GetBaseException()); } }
public static TResult EndAsyncResult <TResult>(IAsyncResult asyncResult) { Task <TResult> task = asyncResult as Task <TResult>; if (task == null) { throw Fx.Exception.AsError(new ArgumentException(CommonResources.InvalidAsyncResult)); } try { return(task.Result); } catch (AggregateException ae) { ExceptionDispatcher.Throw(ae.GetBaseException()); // Dummy Code throw ae.GetBaseException(); } }