2010-06-17 156 views
1

我试图在单独的窗体(progressForm)中显示进度条(marque),而我在后台进行一些计算。触发器Backgroundworker已完成的事件

我知道这样做的典型方式是在后台工作者中包含计算并在主线程中显示progressForm。这种方法将会导致我的应用程序出现很多同步问题,因此我在后台工作进程中使用progressForm.ShowDialog()显示progressForm。但我需要在应用程序中触发Completed事件来关闭表单。

这可能吗?

在此先感谢。

回答

1

一旦您的背景工作者的进度达到100%,背景工作者的RunWorkerCompleted事件将触发。

编辑 - 增加了代码示例

Dim WithEvents bgWorker As New BackgroundWorker With { _ 
    .WorkerReportsProgress = True, _ 
    .WorkerSupportsCancellation = True} 

    Private Sub bgWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork 
     For i As Integer = 0 To 100 
      'Threw in the thread.sleep to illustrate what's going on. Otherwise, it happens too fast. 
      Threading.Thread.Sleep(250) 
      bgWorker.ReportProgress(i) 
     Next 
    End Sub 

    Private Sub bgWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged 
     If e.ProgressPercentage Mod 10 = 0 Then 
      MsgBox(e.ProgressPercentage.ToString) 
     End If 
    End Sub 

    Private Sub bgWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted 
     MsgBox("Done") 
    End Sub 
相关问题