0

我正在尝试使用 Entity-Framework 5.0 Code fast 和 Reactive Extensions (Rx) 从数据库中检索数据。我正在为此编写以下代码。

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;
    }

但它向我显示了这个警告:

'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.'

在运行时我得到一个异常:

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

请建议我如何在我的 WPF 应用程序中快速正确地在 EF5.0 代码中使用 Reactive Extensions (Rx) 或任何异步调用数据库的方式。

4

1 回答 1

1

您需要调用 ObserveOnDispatcher 而不是 SubscribeOnDispatcher。见这里

于 2013-02-17T13:10:19.823 回答