GameTimer crashes when stopping timer in Update or Draw method

Topics: General
Aug 9, 2012 at 10:01 AM

I am just trying to figure out whether this is meant to be done in some other way. I build my own Game class which is now working with either Xna or MonoGame.

When the user presses the Escape key, the game is stopped and disposed, which also includes disposing the GameTimer object. Since the keypress is caught in Update, the GameTimer is still running though the list of timers:

            foreach (var timer in _updateTimers)
                timer.DoUpdate(elapsed);

Since on dispose this list is changed, the method crashes with an exception, that the list has been changed.

Am I missing something and the timer should be disposed of somewhere else? If so, where would be a good way to do that? Otherwise I would say this is a bug.

Coordinator
Aug 9, 2012 at 10:11 AM

Yea... looks like a bug.

We probably should be making a copy of the _updateTimers list and iterating over that, skipping any disposed timers.

Also note I'm still experimenting with extending the Game class to work within a XAML application which might make the GameTimer stlye of integration redundant.

 - Tom

 

Aug 9, 2012 at 10:17 AM

Thanks for the quick reply. I guess I will make this change locally for now and compile the source myself again. Apart from that I would welcome the possibility to use the Game class from within a XAML application.

- Jens

Aug 9, 2012 at 6:01 PM
Edited Aug 9, 2012 at 6:01 PM

Temporarily, if you switch that to a for and do a null check it'll skip past it without a hitch.