Windows Phone: how to tell when Deployment.Current.Dispatcher.BeginInvoke has completed?

前端 未结 1 1149
囚心锁ツ
囚心锁ツ 2021-01-06 18:14

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

相关标签:
1条回答
  • 2021-01-06 18:40

    It's very easy ;)

    // 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
    

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

    0 讨论(0)
提交回复
热议问题