开发者

Rx for .NET - What happened to Scheduler.Dispatcher?

开发者 https://www.devze.com 2023-03-15 05:57 出处:网络
I\'m trying to work through Dan Sullivan\'s Rx Extensions training course on PluralSight.It\'s excellent stuff but unfortunately Rx seems to have already been changed, even though the course was only

I'm trying to work through Dan Sullivan's Rx Extensions training course on PluralSight. It's excellent stuff but unfortunately Rx seems to have already been changed, even though the course was only published a month ago.

Most of the changes are trivial to work out (change from three dlls to a single dll, change in namespaces used etc) but I'm struggling to understand what I should use in place of Scheduler.Dispatcher in Dan's example. I can't see anything obvious in the properties tha开发者_Go百科t are available in the Scheduler.

Here's the code I'm trying to get working with the (refactored?) Rx library (the currenly stable version v1.0.10605)

var query = from number in Enumerable.Range(1, 25) select StringWait(number.ToString());
var observableQuery = query.ToObservable(Scheduler.ThreadPool);
observableQuery.ObserveOn(Scheduler**.Dispatcher**).Subscribe(n => Results.AppendText(string.Format("{0}\n", n)));

What should I be using to invoke the Observer code (Results.AppendText) on the original Dispatcher thread?


The DispatcherScheduler has been moved to the System.Reactive.Windows.Threading assembly. If you are using NuGet, it's in Rx-WPF


As of 2016-11-25, the reference is RX-XAML.

Unfortunately, Microsoft delisted RX v2.2.5 in favour of RX v3.1.0, which is fully cross platform. However, the cross platform libraries do not support WPF. This means that it is now difficult to find the NuGet package which works with WPF.

To work around this, if you are using WPF + .NET 4.5, install any NuGet package with a dependency on RX-XAML. For example, reactiveui-blend depends on RX-XAML v2.2.5, so this will now work:

setClipboard.ObserveOnDispatcher().Subscribe(o =>
{
    ...
});


In Rx for .NET v4+ the assemblies/packages are merged in System.Reactive.

The Dispatcher scheduler can now be found in System.Reactive.Concurrency.DispatcherScheduler.Current

var subscription = observable
  .ObserveOn(System.Reactive.Concurrency.DispatcherScheduler.Current)
  .Subscribe(observer);

Tested in a .NET core 3.1 WPF application

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号