Trigger Backgroundworker Completed event

筅森魡賤 提交于 2019-12-02 13:13:12

问题


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.


回答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


来源:https://stackoverflow.com/questions/3064973/trigger-backgroundworker-completed-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!