コード例 #1
0
        private void CompleteTask(bool timedOut, bool syncTimeoutCaller)
        {
            if (Interlocked.Exchange(ref _completionMethodLock, 1) != 0)
            {
                return;
            }

            bool needSetupThreadContext;
            bool responseEnded = false;

            if (timedOut)
            {
                needSetupThreadContext = !syncTimeoutCaller;
            }
            else
            {
                _completedSynchronously = _asyncResult.CompletedSynchronously;
                needSetupThreadContext  = !_completedSynchronously;
            }

            // call the completion or timeout handler
            //  when neeeded setup the thread context and lock
            //  catch and remember all exceptions

            HttpApplication app = _taskManager.Application;

            try {
                if (needSetupThreadContext)
                {
                    using (app.Context.SyncContext.AcquireThreadLock()) {
                        ThreadContext threadContext = null;
                        try {
                            threadContext = app.OnThreadEnter();
                            if (timedOut)
                            {
                                if (_timeoutHandler != null)
                                {
                                    _timeoutHandler(_asyncResult);
                                }
                            }
                            else
                            {
                                _endHandler(_asyncResult);
                            }
                        }
                        finally {
                            if (threadContext != null)
                            {
                                threadContext.DisassociateFromCurrentThread();
                            }
                        }
                    }
                }
                else
                {
                    if (timedOut)
                    {
                        if (_timeoutHandler != null)
                        {
                            _timeoutHandler(_asyncResult);
                        }
                    }
                    else
                    {
                        _endHandler(_asyncResult);
                    }
                }
            }
            catch (ThreadAbortException e) {
                _error = e;

                HttpApplication.CancelModuleException exceptionState = e.ExceptionState as HttpApplication.CancelModuleException;

                // Is this from Response.End()
                if (exceptionState != null && !exceptionState.Timeout)
                {
                    // Mark the request as completed
                    using (app.Context.SyncContext.AcquireThreadLock()) {
                        // Handle response end once. Skip if already initiated (previous AsyncTask)
                        if (!app.IsRequestCompleted)
                        {
                            responseEnded = true;
                            app.CompleteRequest();
                        }
                    }

                    // Clear the error for Response.End
                    _error = null;
                }

                // ---- the exception. Async completion required (DDB 140655)
                Thread.ResetAbort();
            }
            catch (Exception e) {
                _error = e;
            }


            // Complete the current async task
            _completed = true;
            _taskManager.TaskCompleted(_completedSynchronously /*onCallerThread*/); // notify TaskManager

            // Wait for pending AsyncTasks (DDB 140655)
            if (responseEnded)
            {
                _taskManager.CompleteAllTasksNow(false /*syncCaller*/);
            }
        }
コード例 #2
0
        private void CompleteTask(bool timedOut, bool syncTimeoutCaller)
        {
            bool flag;

            if (Interlocked.Exchange(ref this._completionMethodLock, 1) != 0)
            {
                return;
            }
            bool flag2 = false;

            if (timedOut)
            {
                flag = !syncTimeoutCaller;
            }
            else
            {
                this._completedSynchronously = this._asyncResult.CompletedSynchronously;
                flag = !this._completedSynchronously;
            }
            HttpApplication application = this._taskManager.Application;

            try
            {
                if (flag)
                {
                    lock (application)
                    {
                        HttpApplication.ThreadContext context = null;
                        try
                        {
                            context = application.OnThreadEnter();
                            if (timedOut)
                            {
                                if (this._timeoutHandler != null)
                                {
                                    this._timeoutHandler(this._asyncResult);
                                }
                            }
                            else
                            {
                                this._endHandler(this._asyncResult);
                            }
                        }
                        finally
                        {
                            if (context != null)
                            {
                                context.Leave();
                            }
                        }
                        goto Label_0141;
                    }
                }
                if (timedOut)
                {
                    if (this._timeoutHandler != null)
                    {
                        this._timeoutHandler(this._asyncResult);
                    }
                }
                else
                {
                    this._endHandler(this._asyncResult);
                }
            }
            catch (ThreadAbortException exception)
            {
                this._error = exception;
                HttpApplication.CancelModuleException exceptionState = exception.ExceptionState as HttpApplication.CancelModuleException;
                if ((exceptionState != null) && !exceptionState.Timeout)
                {
                    lock (application)
                    {
                        if (!application.IsRequestCompleted)
                        {
                            flag2 = true;
                            application.CompleteRequest();
                        }
                    }
                    this._error = null;
                }
                Thread.ResetAbort();
            }
            catch (Exception exception3)
            {
                this._error = exception3;
            }
Label_0141:
            this._completed = true;
            this._taskManager.TaskCompleted(this._completedSynchronously);
            if (flag2)
            {
                this._taskManager.CompleteAllTasksNow(false);
            }
        }