我剛開始學習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