Esempio n. 1
0
        protected void Complete(bool completedSynchronously)
        {
            if (IsCompleted)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(GetType())));
            }



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

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

            if (_callback != null)
            {
                try
                {
                    if (VirtualCallback != null)
                    {
                        VirtualCallback(_callback, this);
                    }
                    else
                    {
                        _callback(this);
                    }
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, e));
                }
            }
        }
Esempio n. 2
0
        protected void Complete(bool completedSynchronously)
        {
            if (this.isCompleted)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(GetType())));
            }

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

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

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

            if (this.callback != null)
            {
                try
                {
                    if (VirtualCallback != null)
                    {
                        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(InternalSR.AsyncCallbackThrewException, e));
                }
#pragma warning restore 1634
            }
        }
Esempio n. 3
0
 protected void Complete(bool completedSynchronously)
 {
     if (!this.isCompleted)
     {
         this.completedSynchronously = completedSynchronously;
         if (this.OnCompleting != null)
         {
             try
             {
                 this.OnCompleting(this, this.exception);
             }
             catch (Exception exception1)
             {
                 Exception exception = exception1;
                 if (!Fx.IsFatal(exception))
                 {
                     this.exception = exception;
                 }
                 else
                 {
                     throw;
                 }
             }
         }
         if (!completedSynchronously)
         {
             lock (this.ThisLock)
             {
                 this.isCompleted = true;
                 if (this.manualResetEvent != null)
                 {
                     this.manualResetEvent.Set();
                 }
             }
         }
         else
         {
             this.isCompleted = true;
         }
         if (this.callback != null)
         {
             try
             {
                 if (this.VirtualCallback == null)
                 {
                     this.callback(this);
                 }
                 else
                 {
                     this.VirtualCallback(this.callback, this);
                 }
             }
             catch (Exception exception3)
             {
                 Exception exception2 = exception3;
                 if (!Fx.IsFatal(exception2))
                 {
                     throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, exception2));
                 }
                 else
                 {
                     throw;
                 }
             }
         }
         return;
     }
     else
     {
         throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(this.GetType())));
     }
 }