0

I have written below code. When I click one button 1 window freezes even though I used threading. Button 2 click event starts when button 1 task gets completed. I want to run and start processing as soon as I click on the button. Even when I click on button 1 I can't move windows form... I am using Visual Studio 2008

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
4

1 回答 1

2

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

于 2013-10-04T06:38:14.170 回答