问题
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()?
回答1:
ObservableOnDispatcher
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.
回答2:
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.
回答3:
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()
.
来源:https://stackoverflow.com/questions/6043488/observeondispatcher-not-working