Interface Marshalled for a Different Thread in C# on Windows 8

折月煮酒 提交于 2019-12-24 11:11:59

问题


I'm working on a Windows 8 app. While the app is running, I'm trying to determine when a user's internet connectivity is restored. In an attempt to do this, I'm listening to the NetworkChange_NetworkAddressChanged event. When that event is fired, I try to update my UI and execute my query to my web service via a method called GetLatestData. When I attempt to call this method from within the NetworkChange_NetworkAddressChanged event, I currently get an error that says:

The application called an interface that was marshalled for a different thread.

How do I overcome this error to update my UI and hit a web service from the NetworkChange_NetworkAddressChanged event?


回答1:


    NetworkInformation.NetworkStatusChanged += (sender) =>
    {
        Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, Update);
    };

Sample above calls (marshals) Update function using UI thread.




回答2:


The same way you do that in WIndows 7 - you MARSHAL back into the UI thread using the UI thread Dispatcher.



来源:https://stackoverflow.com/questions/11122312/interface-marshalled-for-a-different-thread-in-c-sharp-on-windows-8

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