コード例 #1
0
ファイル: ChoWorkItem.cs プロジェクト: lanicon/Cinchoo
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Captures the current thread context
        /// </summary>
        /// <returns></returns>
        public static ChoCallerThreadContext Capture()
        {
            ChoCallerThreadContext callerThreadContext = new ChoCallerThreadContext();

            Thread thread = Thread.CurrentThread;

            callerThreadContext._culture   = thread.CurrentCulture;
            callerThreadContext._cultureUI = thread.CurrentUICulture;
            callerThreadContext._principal = Thread.CurrentPrincipal;
            callerThreadContext._context   = Thread.CurrentContext;
            return(callerThreadContext);
        }
コード例 #3
0
        /// <summary>
        /// Applies the thread context stored earlier
        /// </summary>
        /// <param name="callerThreadContext"></param>
        public static void Apply(ChoCallerThreadContext callerThreadContext)
        {
            Thread thread = Thread.CurrentThread;

            thread.CurrentCulture   = callerThreadContext._culture;
            thread.CurrentUICulture = callerThreadContext._cultureUI;
            Thread.CurrentPrincipal = callerThreadContext._principal;

            // Uncomment the following block to enable the Thread.CurrentThread

            /*
             *          if (null != _fieldInfo)
             *          {
             *              _fieldInfo.SetValue(
             *                  Thread.CurrentThread,
             *                  callerThreadContext._context);
             *          }
             */
        }
コード例 #4
0
ファイル: ChoWorkItem.cs プロジェクト: lanicon/Cinchoo
        /// <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);
        }