ittay
ittay

Reputation: 689

ObserveOnDispatcher not working

I have 2 threads, WPF+PIPE. I register the from the WPF on the pipe rx event. when using ObserveOnDispatcher() the registered handler is not called, when removing the ObserveOnDispatcher() it is called on the pipe thread. Does anyone have ideas why it is not called at all when using ObserveOnDispatcher()?

Upvotes: 4

Views: 5590

Answers (3)

Richard Szalay
Richard Szalay

Reputation: 84744

DispatcherObservable.ObserveOnDispatcher takes the dispatcher of the current thread at the time when it is called. If you call it from a background thread, it will look for a dispatcher on that thread (if it has one).

If you want to call back to the UI thread, you'll need to get the IScheduler from Scheduler.Dispatcher while on the UI thread (like at the start of the application) and pass that instance to your background thread. You can then use ObserveOn(dispatcherSchedulerInstance) to schedule back to the UI thread.

Upvotes: 8

Tim Sylvester
Tim Sylvester

Reputation: 23138

In addition to ObserveOnDispatcher() using the current dispatcher rather than the "main" UI dispatcher, I ran into this even when using ObserveOn() with a specific, previously-captured dispatcher scheduler.

The issue turned out to be that using some observable methods, in particular the Buffer() overloads with a time period, disconnects the observable from its previous ObserveOn context and cause it to be observed from a separate "timer" task. As a result, the ObserveOn must be done after the call to Buffer().

Upvotes: 0

aL3891
aL3891

Reputation: 6275

Can you post some code? :)

In general i'd look for any place where you might be blocking the ui thread, since the wpf dispatcher is single threaded, a blocking operation on the dispatcher will cause your subscribe callback to never be executed.

Upvotes: 0

Related Questions