1

I am trying to display the progress bar(marque) in a separate form (progressForm) while i do some calculation in background.

I know the typical way of doing it is to include the calculation in background worker and show progressForm in main thread. This approach how ever will lead to lot of synch issues in my application hence I am showing the progressForm using progressForm.ShowDialog() inside the background worker process. But I need to trigger the Completed event with in the application to close the form.

Is this possible?

Thanks in advance.

fireBand
  • 947
  • 7
  • 23
  • 42

1 Answers1

1

Once your backgroundworker's progress reaches 100% the RunWorkerCompleted event for the backgroundworker will fire.

Edit - Added code sample

    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
brad.huffman
  • 1,281
  • 8
  • 12
  • This is inaccurate statement. The RunWorkerCompleted event will also fire when DoWork exits, even if you do not report any progress. In fact, ProgressChanged will be fired automatically with progress = 100% when DoWork exits. – OGCJN Jan 09 '21 at 17:38
  • Update: This is false statement. If you report progress as 100% before the DoWork exits - the RunWorkerCompleted event won't fire. The RunWorkerCompleted event will fire when DoWork exits, and it does not matter if you do report any progress. In fact, ProgressChanged will be fired automatically with progress = 100% when DoWork exits. I've now tested it myself in C++. – OGCJN Jan 09 '21 at 17:45