问题
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