【问题标题】:VB.Net Multithreading Windows FormVB.Net 多线程 Windows 窗体
【发布时间】:2013-10-04 06:04:17
【问题描述】:

我写了下面的代码。当我单击一个按钮时,即使我使用了线程,1 窗口也会冻结。按钮 2 的点击事件在按钮 1 任务完成时开始。我想在单击按钮后立即运行并开始处理。即使我单击按钮 1,我也无法移动窗体... 我正在使用 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

【问题讨论】:

  • 你应该使用 Backgrounworker 类。

标签: vb.net multithreading


【解决方案1】:

不要使用普通的线程结构,而是使用 backgroundworker 类。这将使 UI 保持响应。类似的问题被问到here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2015-03-17
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多