예제 #1
0
        private void BackgroundThreadMethodInner()
        {
            // Notify listeners that the algorithm is starting.
            OnUpdateEvent();

            _prevUpdateGeneration = 0;
            _prevUpdateTimeTick   = DateTime.UtcNow.Ticks;

            for (;;)
            {
                _ea.PerformOneGeneration();

                if (_isUpdateDueFn())
                {
                    _prevUpdateGeneration = _ea.Stats.Generation;
                    _prevUpdateTimeTick   = DateTime.UtcNow.Ticks;
                    OnUpdateEvent();
                }

                // Check if a pause has been requested.
                // Note. Access to the flag is not thread synchronized, but it doesn't really matter if
                // we miss it being set and perform one other generation before pausing.
                if (_pauseRequestFlag || _ea.Stats.StopConditionSatisfied)
                {
                    // Signal to any waiting thread that we are pausing.
                    _awaitPauseEvent.Set();

                    // Test for terminate signal.
                    if (_terminateFlag)
                    {
                        _runState = RunState.Terminated;
                        OnUpdateEvent();
                        return;
                    }

                    // Reset the pause flag, update RunState, and notify any listeners of the state change.
                    _pauseRequestFlag = false;
                    _runState         = RunState.Paused;
                    OnUpdateEvent();

                    // Wait indefinitely for a signal to wake up and continue.
                    _awaitRestartEvent.WaitOne();

                    // Test for terminate signal.
                    if (_terminateFlag)
                    {
                        OnUpdateEvent();
                        return;
                    }

                    // Notify any listeners of the state change.
                    OnUpdateEvent();
                }
            }
        }
        private void AlgorithmThreadMethod()
        {
            try
            {
                _prevUpdateGeneration = 0;
                _prevUpdateTimeTick   = DateTime.Now.Ticks;

                for (;;)
                {
                    _currentGeneration++;
                    _ea.PerformOneGeneration();

                    if (UpdateTest())
                    {
                        _prevUpdateGeneration = _currentGeneration;
                        _prevUpdateTimeTick   = DateTime.Now.Ticks;
                        OnUpdateEvent();
                    }

                    // Check if a pause has been requested.
                    // Access to the flag is not thread synchronized, but it doesn't really matter if
                    // we miss it being set and perform one other generation before pausing.
                    if (_pauseRequestFlag || _ea.EAStats.StopConditionSatisfied)
                    {
                        // Signal to any waiting thread that we are pausing
                        _awaitPauseEvent.Set();

                        // Test for terminate signal.
                        if (_terminateFlag)
                        {
                            return;
                        }

                        // Reset the flag. Update RunState and notify any listeners of the state change.
                        _pauseRequestFlag = false;
                        _runState         = RunState.Paused;
                        OnUpdateEvent();
                        OnPausedEvent();

                        // Wait indefinitely for a signal to wake up and continue.
                        _awaitRestartEvent.WaitOne();
                    }
                }
            }
            catch (ThreadAbortException)
            {   // Quietly exit thread.
            }
        }