我有一个大型应用程序,我想在其中实现加载屏幕。每次用户请求大量数据时都会弹出。当数据库检索数据时,用户会看到一个带有“请稍候”文本的小表单和一个类似gif进度条的东西。
我遇到的问题是我使用backgroundworker在第二个线程中启动此表单。这很好,我看到它弹出,但它在装载完成时不会关闭。我使用了Microsoft MSDN提供的代码。这是我现在的代码,但结束时从未发生过。我调试了代码,看到bgw.cancellationpending永远不会变为true,即使我调用了CancelAsync()方法。
在数据库调用之前调用showLoading,在数据库调用完成后调用cancelLoading()。
出了什么问题,我应该改变什么?
我的代码:
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.ShowDialog()
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
答案 0 :(得分:4)
您的问题似乎是 loadingScreen.ShowDialog()。
使用 loadingScreen.Show(),只要对话框保持打开状态,.ShowDialog()就会阻止执行。