【问题标题】:C#: Calling method in another thread [duplicate]C#:在另一个线程中调用方法[重复]
【发布时间】:2011-09-25 13:16:52
【问题描述】:

可能重复:
How to update GUI from another thread in C#?

以下场景: 我有一个带有一些 GUI 元素(winforms)的类。这个类有一个更新方法,可以改变控件上的东西。 我也有一个 FileSystemWatcher。每当文件更改时,此对象都会给我一个回调。在这种情况下,我调用 update 方法。

正如您可能猜到的那样,这会使应用程序崩溃。原因:来自 FileSystemWatcher 的回调位于创建控件的另一个线程中。如果我随后调用更新方法,它就无法访问控件。

有什么方法可以解决这个问题?谢谢!

【问题讨论】:

  • 对于FSW来说是微不足道的,分配SynchronizingObject属性。

标签: c# winforms multithreading


【解决方案1】:

您应该致电Control.InvokeBeginInvoke,请参阅深入参考Here

【讨论】:

    【解决方案2】:

    这个问题的最高投票答案看起来可能会奏效:

    C# Windows Forms Application - Updating GUI from another thread AND class?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多