How can I update UI when Observable.Context is not available?
-
21-09-2019 - |
Question
I have a WinFrom App, use synchronous method to download string from a url, and use Rx ToAsync Method to make it asynchronous and get the observable result, and when the result comes I show it on the Form.
Yesterday, I updated Rx to the latest release, and it was told that "Observable does not contain a definition of Context". I tried comment this line, the codes threw an exception that "Cross-thread operation not valid: Control 'tbx_Reference' accessed from a thread other than the thread it was created on."
I want to show the asynchronous result using Subscribe method. How can I fix this problem? thanks very much.
public static IObservable<TResult> DoWorkAsync(TParameter parameter,
Func<TParameter,TResult> actionSync)
{
Observable.Context = SynchronizationContext.Current;
Func<TParameter, IObservable<TResult>> ActionAsync = actionSync.ToAsync();
IObservable<TResult> results = from result in ActionAsync(parameter)
select result;
return results;
}
Solution
For your return statement, try:
return results.ObserveOnDispatcher();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow