为类中的sub启动一个线程

时间:2017-12-18 19:27:08

标签: vb.net thread-safety

我想要做的是启动一个类中的Sub的线程,它将改变form1的label1.text。显然这段代码不起作用。

Public Class Form1

    Public Class test
        Public Sub doit()
            Form1.chan("hello")
        End Sub
    End Class
    Delegate Sub changeit(ByVal test As String)

    Public Sub chan(ByVal test As String)
        If Me.Label1.InvokeRequired Then
            Dim d As New changeit(AddressOf chan)
            Me.Invoke(d, {test})
        Else
            Me.Label1.Text = test
            Label1.Refresh()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As New test
        Dim thread As New Threading.Thread(AddressOf a.doit)
        thread.Start()
    End Sub
End Class

0 个答案:

没有答案