Windows Phone的:如何判断何时Deployment.Current.Dispatcher.BeginInvoke已完成? [英] Windows Phone: how to tell when Deployment.Current.Dispatcher.BeginInvoke has completed?

查看:234
本文介绍了Windows Phone的:如何判断何时Deployment.Current.Dispatcher.BeginInvoke已完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个页面在WP7应用程序通过将数据加载到部分后台线程,而不是运行在页面加载时的前景更加敏感的用户界面。

I'm trying to make the UI of a page in a WP7 application more responsive by putting the data loading portion into a background thread rather than running in the foreground when the page loads.

线程代码基本上是致力于通过一些数据,并增加了项目的观察的集合;为了避免异常的问题,我执行是这样的:

The thread code essentially works through some data and adds items to an observable collection; in order to avoid exception issues, I execute something like:

Deployment.Current.Dispatcher.BeginInvoke(() => { _events.Add(_newItem); });



因此​​,该项目的除了集合中的UI线程完成。

so that the addition of the item to the collection is done in the UI thread.

现在我打的问题是,该代码的后续部分需要执行的foreach 上,以收集找出其中插入新的项目,而不是仅仅将其添加。不幸的是,我发现的是,UI线程有时可以执行其加入,而我在foreach循环中,瞬间打破在foreach。

The problem I'm now hitting is that a subsequent part of the code needs to perform a foreach on the collection in order to figure out where to insert a new item rather than just add it. Unfortunately, what I'm finding is that the UI thread can sometimes perform its Add while I'm in the foreach loop, instantly breaking the foreach.

从阅读中,我已经做了,它看起来像一个办法是,直到UI一块做是为了调用EndInvoke(),以阻止后台线程。不幸的是,它看起来像WP7泰华/ Silverlight实现不支持EndInvoke会。

From the reading I've done, it looks like one approach would be to call EndInvoke() in order to block the background thread until the UI piece is done. Unfortunately, it looks like thw Wp7/Silverlight implementation doesn't support EndInvoke.

这是我怎么能检查添加完毕之前,我开始在foreach有什么建议?

Any suggestions on how I can check that the Add has been completed before I start the foreach?

感谢。

飞利浦

推荐答案

这很容易;)

// must be executed in background
foreach (Item item in Items)
{
    EventWaitHandle Wait = new AutoResetEvent(false);
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        _events.Add(_newItem);
        Wait.Set();
    });
    // wait while item is added on UI
    Wait.WaitOne();
}
// here all items are added

这方法,你可以使用无处不在,你需要同步的背景和UI线程执行

This approach you can use everywhere where you need synchronize background and UI thread execution

这篇关于Windows Phone的:如何判断何时Deployment.Current.Dispatcher.BeginInvoke已完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆