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(); asyncResult.manualResetEvent.Dispose(); } if (asyncResult.exception != null) { // Trace before PrepareForRethrow to avoid weird callstack strings Fx.Exception.TraceException(asyncResult.exception, asyncResult.EventLevel, asyncResult.Activity); ExceptionDispatcher.Throw(asyncResult.exception); } return(asyncResult); }
protected static TAsyncResult End <TAsyncResult>(IAsyncResult result) where TAsyncResult : AsyncResult { if (result == null) { throw new ArgumentNullException(nameof(result)); } TAsyncResult asyncResult = result as TAsyncResult; if (asyncResult == null) { throw new ArgumentException(nameof(result), CommonResources.InvalidAsyncResult); } if (asyncResult.endCalled) { throw 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(); asyncResult.manualResetEvent.Dispose(); } if (asyncResult.exception != null) { ExceptionDispatcher.Throw(asyncResult.exception); } return(asyncResult); }