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

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

            Exception exception = null;
            object    result    = null;

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

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

            SetResult(result, exception);
        }
Example #2
0
        /// <summary>
        /// Initialize the callback holding object.
        /// </summary>
        /// <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(
            WorkItemCallback callback,
            object state,
            bool useCallerContext,
            PostExecuteWorkItemCallback postExecuteWorkItemCallback,
            CallToPostExecute callToPostExecute)
        {
            if (useCallerContext)
            {
                _callerContext = ChoCallerThreadContext.Capture();
            }

            _postExecuteWorkItemCallback = postExecuteWorkItemCallback;
            _callToPostExecute           = callToPostExecute;
            _callback                  = callback;
            _state                     = state;
            _workItemState             = WorkItemState.InQueue;
            _workItemCompleted         = null;
            _workItemCompletedRefCount = 0;
            _workItemResult            = new WorkItemResult(this);
        }