I seem to have a thread issue I think, I have written a timer to return to the main screen if its on any other screen for 5 seconds. This code works fine the first time the ResetScreen() is called, but after it returns to the main screen itself, I can’t navigate to any other screen as it keeps returning to the Timer event handler.
I also tested this without the this.Dispatcher.Invoke and replaced it with a message box, and it just constant pops up about every second.
private void ResetScreen()
{
if (!mainScreen)
{
myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 5000;
myTimer.Start();
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
myTimer.Stop();
this.Dispatcher.Invoke((Action)(() =>
{
// show main screen
}));
}
Edit:
I think its just a timer issue in general. Just can’t seem to stop the timer.
When using
System.Timers.Timer()you need to explicitly set theAutoResetproperty tofalse:Check out the documentation and examples on MSDN.