本文介绍了ObserveOnDispatcher 抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从主线程以外的线程创建一个新的 Timer Observable 并调用 ObserveOnDispatcher 时,我收到以下错误

When I create a new Timer Observable from a thread other than primary thread, and I call ObserveOnDispatcher I get the following error

当前线程没有与之关联的 Dispatcher

我在网上找到了一些类似的讨论,有人建议在 ObserveOn 中使用 DispatcherScheduler.Instance 代替.它有效,但这里是有趣的部分.

I found a few similar discussions online, one guy suggested to use DispatcherScheduler.Instance in ObserveOn instead. It works but here is the interesting part.

以下代码警告您 Instance 属性已过时,我们应该使用 Current 属性

The following code gives you the warning that Instance property is obsolete and we should use Current property

var something = Observable.Timer(TimeSpan.FromSeconds(1.0)).ObserveOn(DispatcherScheduler.Instance)

当我将其更改为使用DispatcherScheduler.Current"时,出现与使用 ObserveOnDispatcher 时相同的异常.

When I change it to use 'DispatcherScheduler.Current', I get the same exception as I was getting on ObserveOnDispatcher.

在不在主线程中创建Observable.Timer 并在 Dispatcher 上观察的最有效方法是什么?

What is the most effective way to create an Observable.Timer while not in a primary thread, and observe that on Dispatcher ?

推荐答案

如果你使用 ReactiveUI,你不应该使用 ObserveOnDispatcher,总是 ObserveOn(RxApp.MainThreadScheduler).此外,Timer 方法本身采用一个 IScheduler,这比在此处使用 ObserveOn 更好

If you're using ReactiveUI, you should never use ObserveOnDispatcher, always ObserveOn(RxApp.MainThreadScheduler). Also, the Timer method itself takes an IScheduler which would be better than using ObserveOn here

这篇关于ObserveOnDispatcher 抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 05:35