views:

436

answers:

3

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");
                  }
              ));
        }
A: 

I would not call it elegant, but you could build your own Window to use as a Messagebox and call it with "Show" instead of "ShowDialog".

jvanderh
+1  A: 

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.

Anderson Imes
A: 

You could call the message box asynchronously using BeginInvoke. I did this recently and blogged about it here: http://www.dmcinfo.com/blog.aspx/articleType/ArticleView/articleId/163/Asynchronous-Message-Box-in-WPF.aspx. Hope this helps.

Eric Anderson