我刚开始学习Reactive Extensions。当我InvalidProgramException
尝试订阅我从事件中获得的 observable 时,我得到了。这是完整的代码:
public MainWindow()
{
var observable = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(
h => this.MouseMove += h, h => this.MouseMove -= h);
observable.Subscribe(x => Debug.WriteLine("hello"));
InitializeComponent();
}
异常详情:
InvalidProgramException: "Common Language Runtime detected an invalid program."
at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, TimeSpan dueTime, Func`3 action)
at System.Reactive.Concurrency.LocalScheduler.Schedule[TState](TState state, Func`3 action)
at System.Reactive.Producer`1.SubscribeRaw(IObserver`1 observer, Boolean enableSafeguard)
at System.Reactive.Producer`1.Subscribe(IObserver`1 observer)
at System.ObservableExtensions.Subscribe[T](IObservable`1 source, Action`1 onNext)
at FunWithReactiveUI.MainWindow..ctor() in c:\...
那么我做错了什么?有什么建议么?谢谢。
我使用Rx 2.0.21114,.Net Framework 4.5。在 4.0 上它工作正常。
编辑:
这个简单的代码我也遇到了同样的异常。Subscribe
.net 4.5 在调试模式下失败。
Observable.Return(1).Subscribe(x => Console.WriteLine(x));