我刚开始学习Reactive Extensions。当我尝试订阅我从事件中获得的观察值时,我得到了InvalidProgramException
。下面是完整的代码:Observable.Subscribe在.NET 4.5上的InvalidProgramException失败
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));
那么,没有调试它工作正常,谢谢。 – 2kay
@ 2kay虽然它处于魔法领域,但有时候干净/重建/重新启动也有诀窍。 – Asti