多线程正常工作但是GUI挂起当新线程在vb中创建时

时间:2017-04-16 08:35:09

标签: vb.net multithreading

我对此vb代码有疑问。我有两个按钮来创建不同的计数线程。我在创建单个线程时使用invoke更新GUI标签它工作正常但是当我按下按钮2创建一个新线程时,两个标签都挂起(新值不更新)。 没有例外,没有错误

Public Class Form1



Dim Thread1 As System.Threading.Thread
Dim Thread2 As System.Threading.Thread
Delegate Sub ThreadDelegateTemplate(ByVal str As String)

Public myThread1 As ThreadDelegateTemplate
Public myThread2 As ThreadDelegateTemplate

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    myThread1 = New ThreadDelegateTemplate(AddressOf updateLabel)
    Thread1 = New System.Threading.Thread(AddressOf upcount)
    Thread1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    myThread2 = New ThreadDelegateTemplate(AddressOf updateLabel2)
    Thread2 = New System.Threading.Thread(AddressOf upcount2)
    Thread2.Start()
End Sub
Private Sub upcount()
    Dim i As Integer = 0
    Do Until i = 10000
        i = i + 1
        Invoke(myThread1, New Object() {Convert.ToString(i)})
    Loop
End Sub
Private Sub updateLabel(ByVal str As String)
    Label1.Text = str
End Sub
Private Sub upcount2()
    Dim i2 As Integer = 0
    Do Until i2 = 10000
        i2 = i2 + 1
        Invoke(myThread2, New Object() {Convert.ToString(i2)})
    Loop
End Sub
Private Sub updateLabel2(ByVal str As String)
    Label2.Text = str
End Sub

End Class

0 个答案:

没有答案