无法从单独的线程更新UI

时间:2012-10-29 22:52:23

标签: vb.net winforms multithreading user-interface

我一直在弄乱这里给出的答案 How to update the GUI from another thread in C#?

我收到错误方法'System.Windows.Forms.Label'从以下代码找不到文字

Private Delegate Sub setControlPropertyThreadSafeDelegate(control As Control, propertyName As String, propertyValue As Object)

Public Sub setControlPropertyThreadSafe(control As Control, propertyName As String, propertyValue As Object)
    If control.InvokeRequired = True Then
        control.Invoke(New setControlPropertyThreadSafeDelegate(AddressOf setControlPropertyThreadSafe), New Object() {control, propertyName, propertyValue})
    Else
        control.GetType().InvokeMember(propertyName, Reflection.BindingFlags.SetProperty, Nothing, control, New Object() {control, propertyName, propertyValue}) 'This is where the error occurs
    End If
End Sub

从一个单独的线程我用以下行调用此方法

UI.setControlPropertyThreadSafe(UI.lblExcel, "Text", "Inserting Data into YTD Template")

非常感谢有关此错误的任何帮助。

1 个答案:

答案 0 :(得分:3)

该行

control.GetType().InvokeMember(propertyName, Reflection.BindingFlags.SetProperty, _
    Nothing, control, New Object() {control, propertyName, propertyValue})

应该是

control.GetType().InvokeMember(propertyName, Reflection.BindingFlags.SetProperty, _
    Nothing, control, New Object() {propertyValue})

InvokeMember的最后一个参数是方法的参数列表,对于属性设置,它只是您要设置的值。您链接的问题是正确的。