Beispiel #1
0
        /// <summary>
        /// Execute the work item
        /// </summary>
        private void ExecuteWorkItem()
        {
            CallerThreadContext ctc = null;

            if (null != _callerContext)
            {
                ctc = CallerThreadContext.Capture(_callerContext.CapturedCallContext, _callerContext.CapturedHttpContext);
                CallerThreadContext.Apply(_callerContext);
            }

            Exception exception = null;
            object    result    = null;

            try
            {
                result = _callback(_state);
            }
            catch (Exception e)
            {
                // Save the exception so we can rethrow it later
                exception = e;
            }

            if (null != _callerContext)
            {
                CallerThreadContext.Apply(ctc);
            }

            SetResult(result, exception);
        }
Beispiel #2
0
        /// <summary>
        /// Execute the work item
        /// </summary>
        private void ExecuteWorkItem()
        {
            CallerThreadContext callerThreadContext = null;

            if (this._callerContext != null)
            {
                callerThreadContext = CallerThreadContext.Capture(this._callerContext.CapturedCallContext, this._callerContext.CapturedHttpContext);
                CallerThreadContext.Apply(this._callerContext);
            }
            Exception exception = null;
            object    result    = null;

            try
            {
                result = this._callback(this._state);
            }
            catch (Exception exception2)
            {
                exception = exception2;
            }
            if (this._callerContext != null)
            {
                CallerThreadContext.Apply(callerThreadContext);
            }
            this.SetResult(result, exception);
        }
Beispiel #3
0
 /// <summary>
 /// Initialize the callback holding object.
 /// </summary>
 /// <param name="workItemsGroup"></param>
 /// <param name="workItemInfo"></param>
 /// <param name="callback">Callback delegate for the callback.</param>
 /// <param name="state">State with which to call the callback delegate.</param>
 ///
 /// We assume that the WorkItem object is created within the thread
 /// that meant to run the callback
 public WorkItem(IWorkItemsGroup workItemsGroup, WorkItemInfo workItemInfo, WorkItemCallback callback, object state)
 {
     this._workItemsGroup = workItemsGroup;
     this._workItemInfo   = workItemInfo;
     if (this._workItemInfo.UseCallerCallContext || this._workItemInfo.UseCallerHttpContext)
     {
         this._callerContext = CallerThreadContext.Capture(this._workItemInfo.UseCallerCallContext, this._workItemInfo.UseCallerHttpContext);
     }
     this._callback       = callback;
     this._state          = state;
     this._workItemResult = new WorkItemResult(this);
     this.Initialize();
 }
Beispiel #4
0
        /// <summary>
        /// Initialize the callback holding object.
        /// </summary>
        /// <param name="workItemsGroup"></param>
        /// <param name="workItemInfo"></param>
        /// <param name="callback">Callback delegate for the callback.</param>
        /// <param name="state">State with which to call the callback delegate.</param>
        ///
        /// We assume that the WorkItem object is created within the thread
        /// that meant to run the callback
        public WorkItem(
            IWorkItemsGroup workItemsGroup,
            WorkItemInfo workItemInfo,
            WorkItemCallback callback,
            object state)
        {
            _workItemsGroup = workItemsGroup;
            _workItemInfo   = workItemInfo;

            if (_workItemInfo.UseCallerCallContext || _workItemInfo.UseCallerHttpContext)
            {
                _callerContext = CallerThreadContext.Capture(_workItemInfo.UseCallerCallContext, _workItemInfo.UseCallerHttpContext);
            }

            _callback       = callback;
            _state          = state;
            _workItemResult = new WorkItemResult(this);
            Initialize();
        }
        /// <summary>
        /// Initialize the callback holding object.
        /// </summary>
        /// <param name="workItemsGroup">The workItemGroup of the workitem</param>
        /// <param name="workItemInfo">The WorkItemInfo of te workitem</param>
        /// <param name="callback">Callback delegate for the callback.</param>
        /// <param name="state">State with which to call the callback delegate.</param>
        ///
        /// We assume that the WorkItem object is created within the thread
        /// that meant to run the callback
        public WorkItem(
            IWorkItemsGroup workItemsGroup,
            WorkItemInfo workItemInfo,
            WorkItemCallback callback,
            object state)
        {
            _workItemsGroup = workItemsGroup;
            _workItemInfo   = workItemInfo;

#if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE)
            if (_workItemInfo.UseCallerCallContext || _workItemInfo.UseCallerHttpContext)
            {
                _callerContext = CallerThreadContext.Capture(_workItemInfo.UseCallerCallContext, _workItemInfo.UseCallerHttpContext);
            }
#endif

            _callback       = callback;
            _state          = state;
            _workItemResult = new WorkItemResult(this);
            Initialize();
        }
	    /// <summary>
	    /// Initialize the callback holding object.
	    /// </summary>
	    /// <param name="workItemsGroup"></param>
	    /// <param name="workItemInfo"></param>
	    /// <param name="callback">Callback delegate for the callback.</param>
	    /// <param name="state">State with which to call the callback delegate.</param>
	    /// 
	    /// We assume that the WorkItem object is created within the thread
	    /// that meant to run the callback
	    public WorkItem(
			IWorkItemsGroup workItemsGroup,
			WorkItemInfo workItemInfo,
			WorkItemCallback callback, 
			object state)
		{
			_workItemsGroup = workItemsGroup;
			_workItemInfo = workItemInfo;

			if (_workItemInfo.UseCallerCallContext || _workItemInfo.UseCallerHttpContext)
			{
				_callerContext = CallerThreadContext.Capture(_workItemInfo.UseCallerCallContext, _workItemInfo.UseCallerHttpContext);
			}

			_callback = callback;
			_state = state;
			_workItemResult = new WorkItemResult(this);
			Initialize();
		}
        /// <summary>
        /// Execute the work item
        /// </summary>
        private void ExecuteWorkItem()
        {
#if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE)
            CallerThreadContext ctc = null;
            if (null != _callerContext)
            {
                ctc = CallerThreadContext.Capture(_callerContext.CapturedCallContext, _callerContext.CapturedHttpContext);
                CallerThreadContext.Apply(_callerContext);
            }
#endif

            Exception exception = null;
            object    result    = null;

            try
            {
                try
                {
                    result = _callback(_state);
                }
                catch (Exception e)
                {
                    // Save the exception so we can rethrow it later
                    exception = e;
                }

                // Remove the value of the execution thread, so it will be impossible to cancel the work item,
                // since it is already completed.
                // Cancelling a work item that already completed may cause the abortion of the next work item!!!
                Thread executionThread = Interlocked.CompareExchange(ref _executingThread, null, _executingThread);

                if (null == executionThread)
                {
                    // Oops! we are going to be aborted..., Wait here so we can catch the ThreadAbortException
                    Thread.Sleep(60 * 1000);

                    // If after 1 minute this thread was not aborted then let it continue working.
                }
            }
            // We must treat the ThreadAbortException or else it will be stored in the exception variable
#if SSHARP
            catch (Exception tae)
#else
            catch (ThreadAbortException tae)
#endif
            {
#if SSHARP
                if (!(tae is System.Threading.ThreadAbortException))
                {
                    throw;
                }
#endif
                tae.GetHashCode();
                // Check if the work item was cancelled
                // If we got a ThreadAbortException and the STP is not shutting down, it means the
                // work items was cancelled.
                if (!SmartThreadPool.CurrentThreadEntry.AssociatedSmartThreadPool.IsShuttingdown)
                {
#if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE)
                    Thread.ResetAbort();
#endif
                }
            }

#if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE)
            if (null != _callerContext)
            {
                CallerThreadContext.Apply(ctc);
            }
#endif

            if (!SmartThreadPool.IsWorkItemCanceled)
            {
                SetResult(result, exception);
            }
        }