31

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 that 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?

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
irascian
  • 1,945
  • 2
  • 15
  • 9

3 Answers3

45

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

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • Thanks. I have that dll referenced but am struggling to understand what I should be using to replace the "ObserveOn" method in the query above to. I can reference a DispatcherScheduler but it has no Subscribe method on it to call. – irascian Jun 26 '11 at 09:10
  • 5
    You can just change it to `ObserveOnDispatcher()`, which is an extension method defined in the `System.Reactive.Windows.Threading` assembly. – Richard Szalay Jun 26 '11 at 09:22
  • Gotcha! Thanks. So my last line becomes observableQuery.ObserveOnDispatcher().Subscribe(n => Results.AppendText(string.Format("{0}\n", n))); – irascian Jun 26 '11 at 09:48
  • 1
    For others who are looking for this I found it packaged under Rx-XAML in NuGet. Looks like there are some WPF helpers as well but these don't seem to be required for ObserveOnDispatcher() or DispatcherScheduler. – jpierson Jul 29 '13 at 18:31
1

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 =>
{
    ...
});
Contango
  • 76,540
  • 58
  • 260
  • 305
1

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

Lakerfield
  • 1,071
  • 1
  • 7
  • 19