安全的跨线程调用错误

时间:2011-03-12 19:56:14

标签: vb.net multithreading

我从后台工作者调用以下代码,但不是设置所需的文本,而是将应用程序标题添加到lisbox 有什么问题

 Private Sub SetStatus(ByVal sStatus As String)
        If Me.lsbLog.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetStatus)
            Me.lsbLog.Invoke(d, New Object() {[Text]})
            '// Me.Invoke(Sub() SetStatus(sStatus))
        Else
            If Mid$(LCase$(sStatus), 1, 4) = "sent" Then
                tslSent.Text = "Sent:" & FormatNumber(lSent, 0, TriState.False)
            Else
                lsbLog.Items.Add(sStatus)
            End If
        End If

    End Sub

1 个答案:

答案 0 :(得分:2)

当您调用委托(Text)时,您将获取表单的...New Object() {[Text]} ...属性。您希望在委托调用中发送sStatus参数:

If Me.lsbLog.InvokeRequired Then
    Dim d As New SetTextCallback(AddressOf SetStatus)
    Me.lsbLog.Invoke(d, New Object() {sStatus})
    ''# ...and so on