Visual Studio 如何在应用程序加载或从数据库中获取大数据时显示加载窗体

vohkndzv  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(113)

请在数据库操作期间加载应用程序时帮助实现此代码

我有这样的代码

Private bgw As New BackgroundWorker
Public Sub showLoading()
    bgw.WorkerSupportsCancellation = True
    
    AddHandler bgw.DoWork, AddressOf bgw_doWork
    AddHandler bgw.RunWorkerCompleted, AddressOf bgw_Complete
    
    If Not bgw.IsBusy = True Then
        bgw.RunWorkerAsync()
    End If
End Sub

Public Sub closeLoading()
    If bgw.WorkerSupportsCancellation = True Then
        bgw.CancelAsync()
    End If
End Sub

Private Sub bgw_doWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    Dim loadingScreen As New frmLoading
    
    
    loadingScreen.Show()
    
    While True
        If Not bgw.CancellationPending Then
            Threading.Thread.Sleep(50)
        Else
            e.Cancel = True
            Exit While
        End If
        End While
    End Sub
    
    Private Sub bgw_Complete(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        Dim lstFrm As New List(Of frmLoading)
        lstFrm = Application.OpenForms.OfType(Of frmLoading)()
        
        If lstFrm.Count > 0 Then
            For Each frm As frmLoading In lstFrm
                frm.Close()
            Next
        End If
    End Sub

请帮助我如何在需要时对多个窗体调用此方法

tkqqtvp1

tkqqtvp11#

问题似乎出在加载Screen.ShowDialog()。
使用loadingScreen.Show()作为.ShowDialog()只要对话框保持打开就阻止执行。

相关问题