/// <summary>
        ///     Removes the thread from our dictionary and asynchronously
        ///     signals it to shut down.
        /// </summary>
        /// <param name="dispatcher">
        ///     The dispatcher associated with the worker thread.
        /// </param>
        private static void DestroyWorkerThread(Dispatcher dispatcher)
        {
            Debug.Assert(_poolThreads.ContainsKey(dispatcher));
            Debug.Assert(_poolThreads[dispatcher].Item2 == 0);

            _poolThreads.Remove(dispatcher);

            // Exit our thread politely.  This is asynchronous, so the
            // worker will exit eventually.
            dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
        }
 private static void ShutdownDispatcher(Dispatcher dispatcher, Countdown countdown)
 {
     EventHandler handler = null;
     if ((dispatcher != null) && !dispatcher.HasShutdownFinished)
     {
         countdown.Increment();
         try
         {
             if (handler == null)
             {
                 handler = (sender, e) => countdown.Decrement();
             }
             dispatcher.ShutdownFinished += handler;
             if (dispatcher.HasShutdownFinished)
             {
                 countdown.Decrement();
             }
             else
             {
                 dispatcher.BeginInvokeShutdown(DispatcherPriority.Send);
             }
         }
         catch
         {
             countdown.Decrement();
         }
     }
 }