Animatroller.Framework.Controller.IntStateMachine.InternalSetState C# (CSharp) Method

InternalSetState() protected method

protected InternalSetState ( int newState ) : void
newState int
return void
        protected void InternalSetState(int newState)
        {
            lock (lockObject)
            {
                if (this.nextState.HasValue)
                    this.nextState = null;

                if (!IsIdle)
                {
                    if (this.currentState.Equals(newState))
                    {
                        // Already in this state
                        log.Info("Already in state {0}", newState);
                        return;
                    }
                }
            }

            InternalHold();

            Sequence.SequenceJob sequenceJob;
            lock (lockObject)
            {
                this.stateConfigs.TryGetValue(newState, out sequenceJob);
            }

            if (sequenceJob != null)
            {
                Task jobTask;
                System.Threading.CancellationTokenSource cancelSource;

                cancelSource = Executor.Current.Execute(jobCancelToken =>
                    {
                        sequenceJob.Run(jobCancelToken, false);

                        lock (lockObject)
                        {
                            this.currentJob = null;
                        }

                        if (!jobCancelToken.IsCancellationRequested)
                        {
                            if (nextState.HasValue)
                                InternalSetState(this.nextState.Value);
                            else
                                Hold();
                        }
                    }, this.name, out jobTask);

                lock (lockObject)
                {
                    this.currentJob = new Tuple<System.Threading.CancellationTokenSource, Task>(
                        cancelSource, jobTask);
                    this.currentState = newState;
                }
            }
            RaiseStateChanged();
        }