示例#1
0
        protected static TAsyncResult End <TAsyncResult>(IAsyncResult result)
            where TAsyncResult : AsyncResult
        {
            if (result == null)
            {
                throw Fx.Exception.ArgumentNull(nameof(result));
            }

            var asyncResult = result as TAsyncResult;

            if (asyncResult == null)
            {
                throw Fx.Exception.Argument(nameof(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);
#else
                Fx.Exception.TraceException(asyncResult._exception, TraceEventType.Verbose);
#endif
                ExceptionDispatcher.Throw(asyncResult._exception);
            }

            return(asyncResult);
        }
        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 !WINDOWS_UWP && !PCL && !NETSTANDARD1_3 // Close does not exist in UWP
                asyncResult.manualResetEvent.Close();
#else
                asyncResult.manualResetEvent.Dispose();
#endif
            }

            if (asyncResult.exception != null)
            {
                // Trace before PrepareForRethrow to avoid weird callstack strings
#if !PCL && !NETSTANDARD1_3
                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);
        }
示例#3
0
        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());
            }
        }
示例#4
0
        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();
            }
        }