Vb.net Net多线程Windows窗体

Vb.net Net多线程Windows窗体,vb.net,multithreading,Vb.net,Multithreading,我已经写了下面的代码。当我点击一个按钮时,窗口冻结,即使我使用了线程。按钮2单击事件在按钮1任务完成时开始。我想在单击按钮后立即运行并开始处理。即使单击按钮1,我也无法移动windows窗体。。。 我正在使用VisualStudio2008 Imports System.Threading Public Class MultiThreading Dim i As Integer Dim i2 As Integer Dim thread As System.Thread

我已经写了下面的代码。当我点击一个按钮时,窗口冻结,即使我使用了线程。按钮2单击事件在按钮1任务完成时开始。我想在单击按钮后立即运行并开始处理。即使单击按钮1,我也无法移动windows窗体。。。 我正在使用VisualStudio2008

Imports System.Threading

Public Class MultiThreading

    Dim i As Integer
    Dim i2 As Integer
    Dim thread As System.Threading.Thread
    Dim thread2 As System.Threading.Thread
    Delegate Sub DelegateCountup()
    Delegate Sub DelegateCountup2()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        thread = New System.Threading.Thread(AddressOf countup)
        thread.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        thread2 = New System.Threading.Thread(AddressOf countup2)
        thread2.Start()
    End Sub

    Private Sub countup()
        If InvokeRequired Then
            Dim d As New DelegateCountup(AddressOf countup)
            Me.Invoke(d)
        Else
            Do Until i = 2000
                i = i + 1
                Label1.Text = i
                Me.Refresh()
            Loop
        End If
    End Sub
    Private Sub countup2()
         If InvokeRequired Then
            Dim d As New DelegateCountup(AddressOf countup2)
            Me.Invoke(d)
         Else
            Do Until i2 = 1000
                i2 = i2 + 1
                Label2.Text = i2
                Me.Refresh()
            Loop
        End If
    End Sub
End Class

使用backgroundworker类,而不是使用普通的线程构造。这将保持UI的响应性。有人问了一个类似的问题

您应该改用BackgroundWorker类。