简体   繁体   中英

UWP ObservableCollection (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

I need for some reason to use ObservableCollection in a thread other than the UI thread. Is it possible?

I have this error:

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

at this line:

observableCollection.Add(new RfcommChatDeviceDisplay(deviceInfo));

It is possible to explicitly request the UI thread to be used:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    // Your code goes here
});

This will run the inner block on the UI thread so you can update the UI from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM