Silverlight:线程/延迟操作/异步调用/事件(Silverlight: Threads / Delayed Actions / Asynchronous invocations/events

编程入门 行业动态 更新时间:2024-10-10 16:21:37
Silverlight:线程/延迟操作/异步调用/事件(Silverlight: Threads / Delayed Actions / Asynchronous invocations/events)

我有以下场景:当用户将鼠标移出弹出窗口时,我想要动画发生,五秒钟之后,我想删除一个PopUp。

这是我期望这样做的代码是:

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; }

第一行“leftPopup = true”很好,但第三行“PopUp.IsOpen = false”给了我一个访问冲突异常,可能是因为这个对象属于GUI线程。 有什么方法可以访问PopUp.IsOpen属性吗? 如果没有,是否有其他方式可以在一段时间后调用事件来执行此操作?

干杯

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

最满意答案

尝试使用PopUp.Dispatcher.Invoke()。 这将把你的回调编组回UI线程。

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

更多推荐

本文发布于:2023-08-07 00:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   事件   操作   Threads   Silverlight

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!