如何在应用程序加载或从数据库获取大型数据时显示加载表单

How Show Loading form whenever Application Loads or Fetch Large Data from Database

提问人:Nikunj Chovatiya 提问时间:9/22/2022 更新时间:10/28/2022 访问量:42

问:

每当我的应用程序在数据库操作时加载时,请帮助实现此代码

我有这样的代码

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

请帮助我如何在需要时将此方法调用到多个表单

vb.net visual-studio visual-studio-2010

评论

0赞 John 9/22/2022
我建议你忘记这一点,改用这个。这将减少现有表单中的代码混乱,并且效果也更好,因为它是模态对话。它显示的表单包括一个可以显示或不显示真实进度的表单,但您可以对其进行修改以显示动画 GIF 或任何您想要的内容。ProgressBarPictureBox

答:

-1赞 Hao Yu-MSFT 10/28/2022 #1

您的问题似乎是 loadingScreen.ShowDialog()。

使用 loadingScreen.Show() 作为 .只要 Dialog 保持打开状态,ShowDialog() 就会阻止执行。