Esempio n. 1
0
 private void HandleProcessExit()
 {
     // The process lock may already be held by this thread since the
     // Process object can call its Exited event handler as a side effect of
     // several operations on the Process object.  This could result in a
     // deadlock if the task's Terminated event waits on some other thread that
     // is also blocked on the process.  So we make it asynchronous instead.
     // Join then blocks on the exitedEvent to restore the required synchronization.
     if (Interlocked.Exchange(ref exited, 1) == 0)
     {
         ThreadPool.QueueUserWorkItem((state) =>
         {
             try
             {
                 WaitForConsoleToBeCompletelyReadOnceProcessHasExited();
                 NotifyTerminated(TaskResult <object> .CreateFromValue(ExitCode));
             }
             finally
             {
                 exitedEvent.Set();
             }
         }, null);
     }
 }
Esempio n. 2
0
        private void Run()
        {
            try
            {
                try
                {
                    try
                    {
                        BeforeTask();

                        ThreadAbortException ex = threadAbortScope.Run(invoker.Invoke);

                        if (ex != null)
                        {
                            NotifyTerminated(TaskResult <object> .CreateFromException(ex));
                        }
                        else
                        {
                            NotifyTerminated(TaskResult <object> .CreateFromValue(invoker.Result));
                        }
                    }
                    finally
                    {
                        AfterTask();
                    }
                }
                catch (Exception ex)
                {
                    NotifyTerminated(TaskResult <object> .CreateFromException(ex));
                }
            }
            catch (Exception ex)
            {
                UnhandledExceptionPolicy.Report("An unhandled exception occurred in a thread task.", ex);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Dispatches notification that the task has terminated and provides its result.
        /// </summary>
        /// <param name="result">The task result.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="result"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the task is not currently running.</exception>
        protected void NotifyTerminated(TaskResult result)
        {
            if (result == null)
                throw new ArgumentNullException("result");

            lock (sequenceLock)
            {
                if (!IsRunning)
                    throw new InvalidOperationException("The task is not currently running.");

                this.result = result;

                OnTerminated();
            }
        }