/// <summary>
        /// Change the state of the algorithm interpreter
        /// </summary>
        /// <param name="source">The source from where we changed the state (an interpreter usually)</param>
        /// <param name="e">The new state</param>
        private void ChangeState(object source, AlgorithmInterpreterStateEventArgs e)
        {
#if !DEBUG
            Task.Run(() =>
            {
#endif
                if (PreviewStateChanged != null)
                {
                    PreviewStateChanged(source, e);
                }

                switch (e.State)
                {
                    case AlgorithmInterpreterState.StoppedWithError:
                    case AlgorithmInterpreterState.PauseWithError:
                    case AlgorithmInterpreterState.PauseBreakpoint:
                        Break(true, e);
                        break;

                    case AlgorithmInterpreterState.Stopped:
                        Stop(true);
                        break;

                    case AlgorithmInterpreterState.Pause:
                        Pause(true);
                        break;

                    case AlgorithmInterpreterState.Preparing:
                        State = AlgorithmInterpreterState.Preparing;
                        break;

                    case AlgorithmInterpreterState.Ready:
                        State = AlgorithmInterpreterState.Ready;
                        break;

                    case AlgorithmInterpreterState.Running:
                        State = AlgorithmInterpreterState.Running;
                        break;

                    case AlgorithmInterpreterState.Log:
                        if (InDebugMode)
                        {
                            //Debug.WriteLine(e.LogMessage);
                        }
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                AddStateChangeHistory(e);

                if (StateChanged != null)
                {
                    StateChanged(source, e);
                }
#if !DEBUG
            });
#endif
        }
        /// <summary>
        /// Add a new state to the historic
        /// </summary>
        /// <param name="algorithmInterpreterStateEventArgs">the new interpreter state</param>
        private void AddStateChangeHistory(AlgorithmInterpreterStateEventArgs algorithmInterpreterStateEventArgs)
        {
            if (_stateChangedHistory.Count > 0)
            {
                var lastAlgorithmInterpreterStateEventArgs = _stateChangedHistory[_stateChangedHistory.Count - 1];
                if (lastAlgorithmInterpreterStateEventArgs != null && lastAlgorithmInterpreterStateEventArgs.State == AlgorithmInterpreterState.Stopped)
                {
                    return;
                }
            }

            _stateChangedHistory.Add(algorithmInterpreterStateEventArgs);
        }
        /// <summary>
        /// Put the program in pause after a breakpoint or an error
        /// </summary>
        private void Break(bool calledByChangeState, AlgorithmInterpreterStateEventArgs algorithmInterpreterState = null)
        {
            if (State == AlgorithmInterpreterState.Running)
            {
                if (algorithmInterpreterState != null)
                {
                    Error = algorithmInterpreterState.Error;
                    DebugInfo = algorithmInterpreterState.DebugInfo;

                    switch (algorithmInterpreterState.State)
                    {
                        case AlgorithmInterpreterState.StoppedWithError:
                            Stop(calledByChangeState, true);
                            break;

                        case AlgorithmInterpreterState.PauseBreakpoint:
                            ProgramInterpreter.Breakpoint();
                            State = AlgorithmInterpreterState.PauseBreakpoint;
                            break;

                        case AlgorithmInterpreterState.PauseWithError:
                            if (!calledByChangeState)
                            {
                                ChangeState(this, new AlgorithmInterpreterStateEventArgs(AlgorithmInterpreterState.Pause));
                            }
                            else
                            {
                                State = AlgorithmInterpreterState.Pause;
                            }
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    if (!calledByChangeState)
                    {
                        ChangeState(this, new AlgorithmInterpreterStateEventArgs(AlgorithmInterpreterState.Pause));
                    }
                    else
                    {
                        State = AlgorithmInterpreterState.Pause;
                    }
                }
            }
        }