vb.net代表似乎没有工作

时间:2014-06-07 13:31:18

标签: vb.net multithreading delegates

在VB.net中,我试图使用委托从一个线程更新表单上的控件,但它不会更改表单上的任何内容。我已经确认它实际上正在接收数据(因此它不是空白的),但由于某种原因它不会将数据发送到控件。

Delegate Sub fupdatedelegate(ByVal itemtoadd As String)
Dim myupdate As fupdatedelegate = AddressOf updatefrmlist

Private Sub updatefrmlist(ByVal itemtoadd As String) 
    With Form1.ListBox1.Items
        .Add(itemtoadd)
    End With
    MsgBox(itemtoadd)
End Sub

并称为

If Form1.ListBox1.InvokeRequired Then
   Form1.ListBox1.BeginInvoke(myupdate)
End If

我怎样才能将它实际添加到ListBox? (这是从模块运行的)

1 个答案:

答案 0 :(得分:1)

尝试修改sub来处理调用。然后用要添加的项目调用它。

Private Sub updatefrmlist(ByVal itemtoadd As String)
    If Me.InvokeRequired Then
        Me.BeginInvoke(myupdate, itemtoadd)
    Else
        With ListBox1.Items
            .Add(itemtoadd)
        End With
        ' MsgBox(itemtoadd)
    End If
End Sub

所有电话都会显示为

 updatefrmlist(somestring)
相关问题