【问题标题】:Multithreading with .NET controls.NET 控件的多线程
【发布时间】:2014-03-16 16:37:08
【问题描述】:

我想了解以下为什么会引发异常:

    Dim t As New Threading.Thread(Sub()
                                      Dim x As New PictureBox
                                      Invoke(Sub() Controls.Add(x))
                                      x.Location = New Point(1, 0) 'here
                                  End Sub)
    t.Start()

注释行抛出异常:

Control accessed from a thread other than the thread it was created on.

但是x在线程t上创建的,不是吗?

如果我在Controls.Add之前进行分配,它会起作用。 Cotrols.Add 是否会以某种方式更改 x 属于哪个线程?为什么?

动机:
我的表单上有 10-20 个 SWF 播放器控件,所有这些控件都同时播放 flash 电影,如果我将它们全部放在一个线程上,它真的会滞后,所以我需要多线程。

【问题讨论】:

    标签: .net multithreading winforms user-controls


    【解决方案1】:

    戴夫, 此错误是因为控件只能从主线程或 UI 线程(创建这些控件的线程)更新。这称为线程亲和性。建议您阅读this 文章以了解更多相关信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      相关资源
      最近更新 更多