VBNet 如何知道后台工作程序是否已被取消

发布于 2024-12-23 13:28:57 字数 578 浏览 2 评论 0原文

目前,我有以下代码: My_BgWorkerB 是我的 BackGround Worker 的名称

    If My_BgWorkerB.IsBusy Then
        If My_BgWorkerB.WorkerSupportsCancellation Then
            My_BgWorkerB.CancelAsync()
        End If

        ' in this part i want to know if the background worker is already stopped
        ' so that i can start it again

        ' My_BgWorkerB.RunWorkerAsync() ' => this should be triggered if the worker
                                        '    has already been stop.

    Else
        My_BgWorkerB.RunWorkerAsync()
    End If

currently, i have this code: My_BgWorkerB is the Name of my BackGround Worker

    If My_BgWorkerB.IsBusy Then
        If My_BgWorkerB.WorkerSupportsCancellation Then
            My_BgWorkerB.CancelAsync()
        End If

        ' in this part i want to know if the background worker is already stopped
        ' so that i can start it again

        ' My_BgWorkerB.RunWorkerAsync() ' => this should be triggered if the worker
                                        '    has already been stop.

    Else
        My_BgWorkerB.RunWorkerAsync()
    End If

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

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

发布评论

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

评论(1

骄傲 2024-12-30 13:28:57

您应该做的是检查 RunWorkerCompletedEventArgs

像这样的事情:

Private Sub My_BgWorkerB_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles My_BgWorkerB.RunWorkerCompleted
    If e.Cancelled Then
       My_BgWorkerB.RunWorkerAsync()  
    End If
End Sub

What you should do is check the RunWorkerCompletedEventArgs Cancelled property in your BGW's RunWorkerCompleted event handler. If it's true restart your BGW using the RunWorkerAsync() method.

Something like this:

Private Sub My_BgWorkerB_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles My_BgWorkerB.RunWorkerCompleted
    If e.Cancelled Then
       My_BgWorkerB.RunWorkerAsync()  
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文