I have an object that has a timer and it throws an event when the timer reaches to 5 minutes. In this event, I call a MessageBox.Show(“Something”) in my MainWindow.xaml.cs.
The problem is that when I call the MessageBox.Show(), the timer stops, until the user hits ok. And I need the timer to keep going even if the user hasn’t clicked ok. Is where a good, elegant way to do this? This is what I’ve tried so far (but didn’t work):
private void OnAlert(object sender, MvpEventArgs e)
{
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
MessageBox.Show("Alert");
}
));
}
What kind of timer are you using? Try a DispatchTimer. Doesn’t make much sense that it would stop. You might have to be explicit about things (calling .Start() again from the completed handler, but the DispatchTimer auto-resets).
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx
The other thing you get with the DispatchTimer is that it’s Tick event is automatically evaluated on the Dispatch queue, so you don’t have to explicitly put it in the Dispatch queue… just call MessageBox.Show yourself. 🙂
Also… what you are doing sounds awful… I’m hoping MessageBox.Show is just your proof of concept and you are going to replace it with SomethingBetterAndNotSoAnnoyingAndModal.Show(), but this is not helpful to your question, just an observation.