简体   繁体   English

.net Observable'OrserveOn'一个后台线程

[英].net Observable 'ObserveOn' a background thread

I am trying to implement a simple Observer pattern using .net Observable class. 我试图使用.net Observable类实现一个简单的Observer模式。 I have code that looks like this: 我的代码看起来像这样:

Observable.FromEventPattern<PropertyChangedEventArgs>(
    Instance.User,
    "PropertyChanged")
          .Where(e => e.EventArgs.PropertyName == "FirstName")
          .ObserveOn(Scheduler.ThreadPool)
          .Subscribe(search => OnFirstNameChanged(search.EventArgs));

Observable.FromEventPattern<PropertyChangedEventArgs>(
    Instance.User,
    "PropertyChanged")
          .Where(e => e.EventArgs.PropertyName == "LastName")
          .ObserveOn(Scheduler.ThreadPool)
          .Subscribe(search => OnLastNameChanged(search.EventArgs));

I want the observers to run on a background thread, but I want them to all run on the same background thread (for our real implementation, it will be too complicated to have every listener on a different thread). 我希望观察者在后台线程上运行,但是我希望它们都在相同的后台线程上运行(对于我们真正的实现,将每个侦听器放在不同的线程上会非常复杂)。

ie I want all of the OnXXXChanged logic to be performed on a thread other than the UI thread, but instead of Observing on the entire threadpool, I want to make sure they run in the correct order, on the same thread. 即我想所有的OnXXXChanged逻辑可能会比在UI线程以外的线程执行,但不是Observing对整个线程池,我要确保他们在正确的顺序运行,在同一个线程。

How should the above be modified? 如何修改上述内容?

Also, on a somewhat related note, are there any good sample code examples using the Observable class to implement this pattern? 另外,在某种程度上相关的说明中,是否有使用Observable类实现此模式的良好示例代码示例?

You should create an EventLoopScheduler and use that single instance in all calls to ObserverOn : 您应该创建一个EventLoopScheduler并在ObserverOn所有调用中使用该单个实例:

var scheduler = new EventLoopScheduler(ts => new Thread(ts));

... .ObserveOn(scheduler). ...

The thread created by the factory method is the thread used to schedule the execution on. 工厂方法创建的线程是用于计划执行的线程。 By leaving the property ExitIfEmpty set to false this thread will not terminate even if there is nothing to do meaning that it will be reused for every call. 通过将属性ExitIfEmpty设置为false即使没有任何操作意味着它将在每次调用中重用,该线程也不会终止。

However, you could also consider using Scheduler.NewThread . 但是,您也可以考虑使用Scheduler.NewThread Using that scheduler will allow the thread to terminate if there is nothing more to do. 如果没有其他任何操作,使用该调度程序将允许线程终止。 When more work is queued up by ObserverOn a new thread will be created but only a single thread should ever exists meaning that you don't have synchronize different observers. ObserverOn将更多工作排队时,将创建一个新线程,但只存在一个线程,这意味着您没有同步不同的观察者。

The threads created by EventLoopScheduler (which is used by Scheduler.NewThread ) are named Event Loop # . EventLoopScheduler创建的线程(由Scheduler.NewThread )被命名为Event Loop # You will see these names in the debugger. 您将在调试器中看到这些名称。

.ObserveOn(Scheduler.ThreadPool) takes a thread scheduler which dictates the thread that the observation runs on. .ObserveOn(Scheduler.ThreadPool)接受一个线程调度程序,它指示观察运行的线程。 It looks like for a single thread you want to use EventLoopScheduler , rather than ThreadPool. 对于单个线程,您希望使用EventLoopScheduler ,而不是ThreadPool。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM