触发Backgroundworker Completed事件 [英] Trigger Backgroundworker Completed event

查看:102
本文介绍了触发Backgroundworker Completed事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在后台进行一些计算时,我试图以单独的形式(progressForm)显示进度条(marque).

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

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

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.

这可能吗?

谢谢.

推荐答案

背景工作人员的进度一旦达到100%,就会触发背景工作人员的RunWorkerCompleted事件.

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

编辑-添加了代码示例

    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

这篇关于触发Backgroundworker Completed事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆