求写一下vb.net双线程的代码:两个按钮和两个label控件,按钮让label自加,按钮互不干扰

2025-05-19 23:12:20
推荐回答(1个)
回答(1):

Imports System
Imports System.Threading
Public Class Form1
    Dim TestThread1, TestThread2 As Thread
    Public Sub TestMethod1()
        Dim i As Integer
        i = 0
        While (i < 10)
            Label1.Text = i
            i += 1
        End While
    End Sub
    Public Sub TestMethod2()
        Dim i As Integer
        i = 0
        While (i < 10)
            Label2.Text = i
            i += 1
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Control.CheckForIllegalCrossThreadCalls = False
        TestThread1 = New Thread(New ThreadStart(AddressOf TestMethod1))
        TestThread1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Control.CheckForIllegalCrossThreadCalls = False
        TestThread2 = New Thread(New ThreadStart(AddressOf TestMethod2))
        TestThread2.Start()
    End Sub
End Class