/// <summary>
        /// 
        /// </summary>
        /// <returns>Returns true if the current processing stage should break</returns>
        private bool CheckForReadyToEndOrRetry()
        {
            if (this._pendingAction != null && !this._pendingActionNeedsBegun)
            {
                if (!this._pendingAction.IsComplete)
                {
                    // nothing to do yet, we are still waiting on the action to complete.  Return
                    return true;
                }

                if (this._pendingAction.Retry)
                {
                    Debug.WriteLine("[PDispatcher] - Resetting Action for Retry");

                    try
                    {
                        // The action as requested that we retry it.
                        this._pendingAction.ResetForRetry();

                        this._pendingActionNeedsBegun = true;

                        // We don't want to process it on this cycle, so return true to signal a break
                        return true;
                    }
                    catch
                    {
                        // If there are any exceptions we need to reset our state
                        this._pendingAction.Dispose();
                        this._pendingAction = null;
                        this._backgroundAction = null;

                        this.DisposeOfVTScopeIfEmptyQueue(false);
                        throw;
                    }
                }

                // The pending action has completed.  We need to finalize it
                try
                {
                    this._pendingAction.End();

                    // Need to deal VT Scope.  Only restore if the queue is empty.
                    // if the queue is not empty, we will deal with it when we go to invoke the next
                    // action.
                    this.DisposeOfVTScopeIfEmptyQueue(false);

                    return true;
                }
                finally
                {
                    this._pendingAction.Dispose();
                    this._pendingAction = null;
                    this._backgroundAction = null;
                }
            }

            // There are no pending actions.
            return false;
        }
        private bool CheckForActionToBegin()
        {
            if (this._pendingAction != null && this._pendingActionNeedsBegun)
            {
                if (this._pendingAction.RequireIdleToPerform && REPlugin.Instance.MonitorManager.CharacterState.Busy.WaitOne(0))
                {
                    // Can't begin invoke yet because this action wants us to wait until we are idle to invoke
                    return true;
                }

                if (!_pendingAction.Ready())
                {
                    // The action has said that for some reason it is not ready to be performed yet.
                    return true;
                }

                try
                {
                    // First begin invoke the action
                    this._pendingAction.Perform();

                    this._backgroundDispatcher.EnqueueAction(this._pendingAction.WaitForOutcome);
                    return true;
                }
                catch
                {
                    // If we hit an exception, we don't want to leave VT in an undesired state.  That could lead to a bot getting killed or something
                    if (this._cachedVTRunScope != null)
                    {
                        this._cachedVTRunScope.Dispose();
                        this._cachedVTRunScope = null;
                    }

                    // If any exceptions, need to reset our state.
                    this._pendingAction = null;
                    this._backgroundAction = null;
                    throw;
                }
                finally
                {
                    this._pendingActionNeedsBegun = false;
                }
            }

            // We didn't do anything
            return false;
        }
        public void Clear()
        {
            lock (this._queueLock)
            {
                try
                {
                    // Nothing to do if the queue is empty
                    if (this.QueueCount == 0)
                    {
                        return;
                    }

                    this._actionQueue.Clear();

                    if (this._pendingAction != null && !this._pendingActionNeedsBegun)
                    {
                        // There is a pending action, let's try and clean it up
                        try
                        {
                            this._pendingAction.End();
                        }
                        finally
                        {
                            this._pendingAction.Dispose();
                            this._pendingAction = null;
                            this._backgroundAction = null;
                        }
                    }
                }
                finally
                {
                    this.DisposeOfVTScopeIfEmptyQueue(true);
                }
            }
        }