触发Backgroundworker Completed事件

发布于 2024-09-06 13:33:48 字数 247 浏览 5 评论 0原文

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

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

这可能吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-09-13 13:33:48

一旦后台工作者的进度达到 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文