views:

567

answers:

1

Hi, I've got the following scenario: when a user moves the mouse out of a popup, I want an animation to happen, and five seconds later, I want to remove a PopUp.

This is the code I expected to do this with is:

private bool leftPopup = false;
public void AnimatePopupOut(object sender, MouseEventArgs e)
{
   myAnim.Begin();
   (new Thread(new ThreadStart(delayedRemovePopup))).Start();
}

private void delayedRemovePopup()
{
   leftPopup = true;
   Thread.Sleep(5000);
   PopUp.IsOpen = false;
}

The first line, "leftPopup = true" is fine, but the third, "PopUp.IsOpen = false" gives me an access violation exception, probably because this object belongs to the GUI thread. Is there any way I can gain access to the PopUp.IsOpen property? If not, is there another way that I can call an event after some time to do this?

Cheers

Nik

+3  A: 

Try using the PopUp.Dispatcher.Invoke(). That will marshal your call back to the UI thread.

Maurice
Thank you very much for that suggestion. Instead of the thread clause I used "Action a = new Action(delayedRemovePopup); PopUp.Dispatcher.BeginInvoke(a);" and that solved it all. :-) Thanks a bunch!
niklassaers-vc
FYI you don't need a temporary variable, just call Popup.Dispatcher.BeginInvoke(delayedRemovePopup);
chakrit