예제 #1
0
 protected void Complete(bool didCompleteSynchronously, Exception e)
 {
     if (!this.TryComplete(didCompleteSynchronously, e))
     {
         throw Fx.Exception.AsError(new InvalidOperationException(SRCore.AsyncResultCompletedTwice(this.GetType())), null);
     }
 }
예제 #2
0
        protected void Complete(bool didCompleteSynchronously)
        {
            if (this.isCompleted)
            {
#if DEBUG
                throw Fx.Exception.AsError(new InvalidOperationException(SRCore.AsyncResultCompletedTwice(this.GetType() + "\r\n First Complete Call Stack: \r\n" + this.completeStack + "\r\n")));
#else
                throw Fx.Exception.AsError(new InvalidOperationException(SRCore.AsyncResultCompletedTwice(this.GetType())));
#endif
            }

#if DEBUG
            this.marker.AsyncResult = null;
            this.marker             = null;
            if (completeStack == null)
            {
                this.completeStack = new StackTrace();
            }
#endif

            this.completedSynchronously = didCompleteSynchronously;
            if (this.OnCompleting != null)
            {
                // Allow exception replacement, like a catch/throw pattern.
                try
                {
                    this.OnCompleting(this, this.exception);
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    this.exception = e;
                }
            }

            if (didCompleteSynchronously)
            {
                // If we completedSynchronously, then there's no chance that the manualResetEvent was created so
                // we don't need to worry about a race
                Fx.Assert(this.manualResetEvent == null, "No ManualResetEvent should be created for a synchronous AsyncResult.");
                this.isCompleted = true;
            }
            else
            {
                lock (this.ThisLock)
                {
                    this.isCompleted = true;
                    if (this.manualResetEvent != null)
                    {
                        this.manualResetEvent.Set();
                    }
                }
            }

            if (this.Callback != null)
            {
                try
                {
                    if (this.VirtualCallback != null)
                    {
                        this.VirtualCallback(this.Callback, this);
                    }
                    else
                    {
                        this.Callback(this);
                    }
                }
#pragma warning disable 1634
#pragma warning suppress 56500 // transferring exception to another thread
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    throw Fx.Exception.AsError(new CallbackException(SRCore.AsyncCallbackThrewException, e));
                }
#pragma warning restore 1634
            }
        }