【问题标题】:Winforms: Observable.Timer throws InvalidOperationExceptionWinforms:Observable.Timer 抛出 InvalidOperationException
【发布时间】:2017-09-19 16:06:32
【问题描述】:

我正在使用 winforms响应式扩展程序
我在表单中使用 Observable.Timer。
当计时器触发时,在回调内部尝试调用任何 winform 控件上的方法时会引发 InvalidOperationException

我该如何解决这个问题?

这是我的代码:

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

【问题讨论】:

  • 什么是表单状态 isDisposed ?检查表单标志

标签: winforms system.reactive


【解决方案1】:

您需要使用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();
        });
    

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2018-07-11
    • 2015-10-03
    相关资源
    最近更新 更多