2010-06-17 154 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 
相關問題