【问题标题】:Shift Control from backgrount thread to main thread将控制从后台线程转移到主线程
【发布时间】:2009-03-17 15:57:35
【问题描述】:

我有监控设备状态的线程(使用 i/o)。这会将事件触发到有关连接状态(已连接、已断开、故障)的几个 UI(表单/对话框..)部分。基于此状态,表单和对话框被销毁、创建、启用和禁用。

我的问题:

   I getting Cross-Thread exception because the thread doing the operations like Dispose the from, create the from .... .  I using smart client, forms and dialogs are part of the WorkItemController and i just terminating that. 

我不想让你使用 Invoke 或 BeginInvoke 。我希望这个线程继续只监视状态。所以这个线程必须将其控制权交给主线程,主线程将关闭表单或创建表单。

只是我想如何将控件从一个线程转移到另一个线程。有什么办法吗?

【问题讨论】:

    标签: c# multithreading


    【解决方案1】:

    最好的解决方案是使用 Invoke 切换回你的主线程。

    或者如果您可以使用在主线程和后台线程之间共享的全局变量,但这意味着您的主线程应该在循环中检查变量,这可能会挂起 GUI。

    或者在主线程中执行时使用 System.Windows.Forms.Timer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      相关资源
      最近更新 更多