ObserveOnDispatcher 不工作
我有 2 个线程,WPF+PIPE。我在管道 rx 事件上从 WPF 注册。 当使用 ObserveOnDispatcher() 时,不会调用注册的处理程序,当删除 ObserveOnDispatcher() 时,它会在管道线程上调用。 有谁知道为什么在使用 ObserveOnDispatcher() 时根本不调用它?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DispatcherObservable.ObserveOnDispatcher
在调用时获取当前线程的调度程序。如果您从后台线程调用它,它将在该线程上查找调度程序(如果有)。如果您想回调 UI 线程,则需要在 UI 线程上从
Scheduler.Dispatcher
获取IScheduler
(就像在应用程序开始时一样)并将该实例传递给您的后台线程。然后,您可以使用 ObserveOn(dispatcherSchedulerInstance) 调度回 UI 线程。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
fromScheduler.Dispatcher
while on the UI thread (like at the start of the application) and pass that instance to your background thread. You can then useObserveOn(dispatcherSchedulerInstance)
to schedule back to the UI thread.你能发布一些代码吗? :)
一般来说,我会寻找任何可能阻塞 ui 线程的地方,因为 wpf 调度程序是单线程的,调度程序上的阻塞操作将导致您的订阅回调永远不会被执行。
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.
除了使用当前调度程序而不是“主”UI 调度程序的
ObserveOnDispatcher()
之外,即使在将ObserveOn()
与特定的先前捕获的程序一起使用时,我也遇到了这个问题调度员调度员。问题是,使用一些可观察方法,特别是 Buffer() 重载一段时间,会断开可观察对象与其之前的
ObserveOn
上下文的连接,并导致它从单独的“计时器”任务中观察。因此,ObserveOn
必须在调用Buffer()
之后完成。In addition to
ObserveOnDispatcher()
using the current dispatcher rather than the "main" UI dispatcher, I ran into this even when usingObserveOn()
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 previousObserveOn
context and cause it to be observed from a separate "timer" task. As a result, theObserveOn
must be done after the call toBuffer()
.