注册 登录
编程论坛 VB.NET论坛

敬请各位高手解决vb.net(VS2010)多线程的问题。谢谢。

HVB6 发布于 2017-07-16 16:53, 2952 次点击
以下代码在(VS2010)测试正常。
Private a As Integer
    Private b As Integer
    Private Sub setA()
        a = 1
    End Sub
    Private Sub setB()
        b = 2
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t1 As Threading.Thread
        Dim t2 As Threading.Thread
        t1 = New Threading.Thread(AddressOf setA)
        t2 = New Threading.Thread(AddressOf setB)
        t1.Start()
        t2.Start()
    End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MessageBox.Show ("a=" & a & ",b=" & b)
    End Sub
问题:如何只用一个按钮可以实现以上代码的功能?以下代码不能实现以上代码的功能。
Private a As Integer
    Private b As Integer
    Private Sub setA()
        a = 1
    End Sub
    Private Sub setB()
        b = 2
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t1 As Threading.Thread
        Dim t2 As Threading.Thread
        t1 = New Threading.Thread(AddressOf setA)
        t2 = New Threading.Thread(AddressOf setB)
        t1.Start()
        t2.Start()
         MessageBox.Show ("a=" & a & ",b=" & b)
    End Sub
2 回复
#2
zhangcanwei2017-07-17 16:57
线程的启动时需要时间的,当线程还没有完全启动时,MessageBox.Show()这句,就开始运行了,那肯定是得不到结果的
#3
无聊时想想你2017-12-22 14:49
回复 楼主 HVB6
Public Class Form1
    Private a As Integer
    Private b As Integer
    Private Sub setA()
        a = 1
    End Sub
    Private Sub setB()
        b = 2
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t1 As Threading.Thread
        Dim t2 As Threading.Thread
        t1 = New Threading.Thread(AddressOf setA)
        t2 = New Threading.Thread(AddressOf setB)
        t1.Start()
        t2.Start()
        t1.Join()
        t2.Join()
        MessageBox.Show("a=" & a & ",b=" & b)
    End Sub

End Class
1