0

I am trying to retrieve data from database using Entity-Framework 5.0 Code fast and Reactive Extensions (Rx). I am writing following code for that.

var obs = DataContext.GetDataContext().BreakdownCauses.OrderBy(x => x.Cause).ToObservable(Scheduler.NewThread);
obs.SubscribeOnDispatcher().Subscribe(x => ItemCollection.Add(slow(x))); 

BreakdownCause slow(BreakdownCause cs)
    {
        System.Threading.Thread.Sleep(500);
        return cs;
    }

But it show me this warning :

'System.Reactive.Concurrency.Scheduler.NewThread' is obsolete: 'This property is no longer supported due to refactoring of the API surface and elimination of platform-specific dependencies. Please add a reference to the System.Reactive.PlatformServices assembly for your target platform and use NewThreadScheduler.Default to obtain an instance of this scheduler type. See http://go.microsoft.com/fwlink/?LinkID=260866 for more information.'

And at RunTime I get an Exception :

The calling thread cannot access this object because a different thread owns it.

Please suggests me how can i use Reactive Extensions (Rx) properly in EF5.0 Code fast in my WPF application or any batter way to call database asynchronously.

Hasanuzzaman
  • 1,822
  • 5
  • 36
  • 54
  • 1
    Did you try changing your code based on the message given to you? – Daniel Kelley Feb 16 '13 at 11:17
  • ya. Now warning is goes away. But Exception is still alive. – Hasanuzzaman Feb 16 '13 at 11:36
  • See the answer to this question - http://stackoverflow.com/questions/7579237/whats-the-difference-between-subscribeon-and-observeon. It seems `SubscribeOnDispatcher` does not necessarily marshal your call to `ItemCollection.Add` back to the UI thread. – Daniel Kelley Feb 16 '13 at 13:40

1 Answers1

1

You need to call ObserveOnDispatcher instead of SubscribeOnDispatcher. See here.

ozczecho
  • 8,649
  • 8
  • 36
  • 42