Windows 8 App The application called an interface that was marshalled for a different thread

为君一笑 提交于 2019-12-06 00:32:49

问题


I am working on a Windows 8 application using c#/XAML. Everything has been working except for this event handler where I get the following error on this line.

await RefreshUserInfoAsync();

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

This Observable Collection is being updated from a push notification handler in the app class and this event handler is in my view model. I'm not using any frameworks like MVVM Light. I looked at some of the other posts on this and tried creating my own Dispatcher Helper but I received a different error where Window.Current.Dispatcher is null. Any ideas how to make this work?



    private async void PushActions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        string action = e.NewItems[0] as string;
        if(action != null)
        {
             await RefreshUserInfoAsync();
        }
    }


    private async Task RefreshUserInfoAsync()
    {
        var userInfos = await SessionRepository.GetSessionUsersWithInfoAsync(SessionGuid, RoundGuid);
        this.UserInfoList = new ObservableCollection<UserInfo>(userInfos);
    }

emphasized text

回答1:


I talked with Jeffrey Richter and he gave me the answer. From the View Model, I can get to the Dispatcher via CoreApplication.MainView.CoreWindow.

Here is the update:

var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
    this.UserInfoList = new ObservableCollection<UserInfo>(userInfos);
});

I hope this helps someone else.

Mike



来源:https://stackoverflow.com/questions/14805543/windows-8-app-the-application-called-an-interface-that-was-marshalled-for-a-diff

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!