本文介绍了ObserveOn(调度)和油门接收油门(......)之差。(...,调度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

IDisposable subscription = myObservable.Throttle(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
                                       .Subscribe(_ => UpdateUi());



正如预期的那样,的updateUI()总会执行主线程上。当我修改代码,

As expected, UpdateUi() will always execute on the main thread. When I change the code to

IDisposable subscription = myObservable.Throttle(TimeSpan.FromMilliseconds(50))
                                       .ObserveOn(RxApp.MainThreadScheduler)
                                       .Subscribe(_ => UpdateUi());



的updateUI()将在执行后台线程。

为什么不是油门(...)。ObserveOn(调度)等同于油门(...,调度程序)

推荐答案

经过一番调查,我相信这由不同版本的接收造成正在使用的比我预期的(我开发的第三方应用程序的插件)的运行时间。

After some investigation I believe this is caused by a different version of Rx being used run time than I expect (I develop a plugin for a third-party application).

我不知道为什么,但似乎默认 RxApp.MainThreadScheduler 未能正确初始化。默认实例是一个 WaitForDispatcherScheduler ()。在这个类中的所有功能依赖 attemptToCreateScheduler

I'm not sure why, but it seems that the default RxApp.MainThreadScheduler fails to initialize correctly. The default instance is a WaitForDispatcherScheduler (source). All functions in this class rely attemptToCreateScheduler:

    IScheduler attemptToCreateScheduler()
    {
        if (_innerScheduler != null) return _innerScheduler;
        try {
            _innerScheduler = _schedulerFactory();
            return _innerScheduler;
        } catch (Exception) {
            // NB: Dispatcher's not ready yet. Keep using CurrentThread
            return CurrentThreadScheduler.Instance;
        }
    }



什么,似乎在我的情况下发生的是, _schedulerFactory()抛出,导致 CurrentThreadScheduler.Instance 来代替返回。

What seems to happen in my case is that _schedulerFactory() throws, resulting in CurrentThreadScheduler.Instance to be returned instead.

通过手动初始化 RxApp.MainThreadScheduler 新SynchronizationContextScheduler(SynchronizationContext.Current)如预期的行为。

By manually initializing the RxApp.MainThreadScheduler to new SynchronizationContextScheduler(SynchronizationContext.Current) behavior is as expected.

这篇关于ObserveOn(调度)和油门接收油门(......)之差。(...,调度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:48