2017-09-16 22 views
1

我正在使用winforms & 被動擴展
我在窗體中使用Observable.Timer。
當計時器觸發時,在回調函數內部InvalidOperationException在嘗試調用任何winform控件上的方法時引發。

我該如何解決這個問題?Winforms:Observable.Timer引發InvalidOperationException

這是我的代碼:

Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(2)) 
    .Subscribe((e) => 
    { 
     XXXXX(); 
    }); 
+0

什麼形式狀態isDisposed?檢查表格標誌 – saeed

回答

3

您需要使用ObserveOn(form)方法。

  1. 安裝NuGet包 「System.Reactive.Windows.Forms」
  2. Subscribe()方法之前添加ObserveOn(form)

    Observable 
        .Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(2)) 
        .ObserveOn(this) 
        .Subscribe((e) => 
        { 
         UpdateProcessStatus(); 
        }); 
    
+0

非常好,您可以稍後將其標記爲答案 – saeed

相關問題