views:

271

answers:

1

hello, 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;
    }
+2  A: 

For your return statement, try:

return results.ObserveOnDispatcher();
Ray Vernagus
It works after I use results.ObserveOnDispatcher(). Thanks very much!
Alexander Guan
Very glad to help!
Ray Vernagus