0

I have a popup in my application for showing messages. Sometimes, two messages get shown too quickly after one another and the second just overrides the first one, making it disappear too quickly.

So what I want is for the next message to wait for the previous message to close. I'm using both a DispatcherTimer and a MouseEvent to close the popup:

popupTimer.Interval = TimeSpan.FromSeconds(5d);
popupTimer.Tick += (sender, args) =>
    { 
        popupMessage.IsOpen = false; 
        popupTimer.Stop();
    };
popupMessage.MouseDown += (sender, args) =>
    {
        popupMessage.IsOpen = false;
        popupTimer.Stop();
    };

Showing the message is done like so:

popupMessage.Dispatcher.Invoke(
    () =>
        {
            popupMessage.IsOpen = true;
            popupTimer.Start();
        });

Now if I place the following loop inside the show delegate

 while(popupMessage.IsOpen) {}

The application just hangs inside it without IsOpen ever changing back to False, even after waiting 5 seconds for the timer to run out. Besides this, I don't like using a blocking while-loop like this.

Is there any way to make my messages wait on each other? I tried using a simple bool instead of PopupMessage's IsOpen, but to no avail.

Davio
  • 4,609
  • 2
  • 31
  • 58
  • You can try invoking one via a Task, and then calling Task.Wait before the next one is invoked. – d.moncada May 17 '13 at 06:27
  • show us more code please, where your messages are appearing from? – Konstantin Chernov May 17 '13 at 06:34
  • 1
    Maybe that's a bit too much said without knowing the context, but it doesn't really sound like a good user experience... Maybe it would make sense to modify your popup to show more than one message? Maybe it makes no sense in your case, but it might be a way to solve your problem and to not create another popup hell... – Marc May 17 '13 at 07:04

1 Answers1

1

Quick hack:

while(popupMessage.IsOpen)
{
    System.Threading.Thread.Sleep(0);
}

The best thing might be pull messages from a queue

Queue<...> messages = new Queue<...>();

...

popupTimer.Tick += (sender, args) =>
{
    if (messages.Count > 0) 
    {
        ... = messages.Dequeue();
    }
    else 
    {
        popupMessage.IsOpen = false;
        popupTimer.Stop();
    }
};

and queue them whenever a message is open.

if (popupMessage.IsOpen) {
    messages.Enqueue(...);
}
Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
  • I incorporated this idea, along with some other fixes, and it seems to work, so I'll accept this as the answer. – Davio May 17 '13 at 09:23