ObserveOnDispatcher not working

半城伤御伤魂 提交于 2021-02-07 03:53:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!