0

我正在使用 Windows 窗体应用程序。单击任何按钮都会变慢。我正在使用 2 个后台工作人员(第一个是对数据进行排序,另一个是通过 Web 服务不断从服务器获取数据)无法理解发生了什么,以前它只使用一个后台工作人员可以正常工作,但是在添加第二个工作人员时应用程序性能减慢,有时它会进入“无响应状态”(双击表单)。任何人都知道它为什么会这样?如果我停止第二个工人,那么它会再次正常工作。

这是我的第二个 BGW 代码

If Not bwProcessData.IsBusy Then
   bwProcessData.RunWorkerAsync()
End If

Private Sub bwProcessData_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwProcessData.DoWork

    While get_all_server_data
        Sync_the_data()
        Application.DoEvents()
    End While

        While IsFutureRecords Or IsPastRecords
            If IsFutureRecords Then
                While IsFutureRecords
                  '' API call for all future date records
                  '' And the update my database
                  '' if no record found then IsFutureRecords = false
                End While
            ElseIf IsPastRecords Then
                While IsPastRecords
                    '' API call for all Past date records
                    '' And the update my database
                    ''  if no record found then IsPastRecords = false
            End While
            End If
        End While
   End Sub


Private Sub bwProcessData_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bwProcessData.RunWorkerCompleted         
            If e.Cancelled Then
            Else
                Dim run_syn_complete_2 As UpdateUIFrom_Background_2 = New UpdateUIFrom_Background_2(AddressOf run_sync_complete_2)
                Dim str_value As String = "Background worker 2"
                Me.Invoke(run_syn_complete_2, str_value)
            End If
End Sub

Public Sub run_sync_complete_2()
    '' Update UI accordingly
End Sub
4

0 回答 0