6

We are using .NET 3.5 and has started using Reactive Extensions. We are using system.Reactive (Runtime Version:v2.0.50727) which is compatible for .NET 3.5.

I'm trying to observe an event on the dispatcher scheduler since I'm using WPF controls(it's winforms shell, with WPF host control embedded), however I could not spot that option on Scheduler class(system.reactive.concurrency.scheduler). Looks like its available from .NET 4.0 onwards. My question is, how do I get this working in .NET 3.5? Note that the call is happening inside my ViewModel and not View.

Code:

 this.ObservePropertyChanged(x => x.Queue)
               //I cant find scheduler dispatcher option, 
               //there are other options such as current, imeediete, new etc.
               .ObserveOn(Scheduler.Dispatcher)
                .Subscribe(RefreshQueues);

Thanks,

-Mike

Mike
  • 3,204
  • 8
  • 47
  • 74

3 Answers3

8

Update: based on Rx 2.2.4.0
DispatcherScheduler for WPF is currently moved into System.Reactive.Windows.Threading namespace.
Using Nuget package, search for Rx-WPF to download the package and use DispatcherScheduler.Current in place of Scheduler.Dispatcher

Hunter
  • 2,370
  • 2
  • 20
  • 24
5

You should be able to do with the RX Winforms extension library from http://www.nuget.org/packages/Rx-WinForms/

this.ObservePropertyChanged(x => x.Queue)
           .ObserveOn(control);

or if you are on the correct thread already.

this.ObservePropertyChanged(x => x.Queue)
           .ObserveOn(SynchronizationContext.Current);
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
3

I think you should be able to use DispatcherScheduler.Current

  • AFAIK, current will only invoke the action on the callback thread, which is a background thread. I need execute the subscription on the UI thread and UI bound collections are getting updated. – Mike Sep 23 '13 at 10:50
  • DispatcherScheduler.Current is what you are describing. It marshals the call to the current ui dispatcher. – Cookingsource Sep 23 '13 at 10:56
  • ahh..now I get it.."dispatcherScheduler" and not "Scheduler"..I tried System.Reactive.Concurrency.DispatcherScheduler.Current, and the line results in compilation error.Are you sure this is availiable in .NET 3.5? – Mike Sep 23 '13 at 11:08
  • 1
    It should be, there's nothing required for DispatcherScheduler implementation that could not be provided at .NET 3.5. The namespace is correct, maybe have some missing references? Seems solved on this question [link](http://stackoverflow.com/questions/6482997/rx-for-net-what-happened-to-scheduler-dispatcher) – Cookingsource Sep 23 '13 at 11:17
  • Make sure you pull in the wpf/xaml nuget packages. Obviously Dispatcher related code is not released on the platform agnostic code base as on servers it doesn't make sense, and on other platforms it has different meaning (winPhone/silverlight) – Lee Campbell Sep 23 '13 at 20:27