VB.net停止后台工作

Mar*_*tes 5 vb.net backgroundworker

我想创建一个按钮,可以阻止我的后台工作程序并结束它正在处理的所有进程.

这是我的示例backgroundworker代码:

       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            Try
                If BackgroundWorker1.IsBusy <> True Then
                    BackgroundWorker1.RunWorkerAsync()
                End If
            Catch ex As Exception
            End Try

        End Sub

        Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

            Dim counter As Integer = 1

            Do

            'updated code with stop function----------------
            BackgroundWorker1.WorkerSupportsCancellation = True
            If BackgroundWorker1.CancellationPending Then
                e.Cancel = True
                ProgressBar1.Value = 0
                Exit Do
            End If
            'updated code with stop function----------------

            ListBox1.Items.Add(counter)

            ProgressBar1.Value = ((counter - 1) / limit) * 100
            counter = counter + 1
            Loop While(counter <= 999999999999999999)

        End Sub

        Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            Try
            Catch ex As Exception
            End Try
        End Sub

        Private Sub BackgroundWorker1_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            Try
            Catch ex As Exception
            End Try
        End Sub

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False    
        End Sub

        'updated code with stop function----------------
        Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
              If BackgroundWorker1.IsBusy Then

                  If BackgroundWorker1.WorkerSupportsCancellation Then                
                     BackgroundWorker1.CancelAsync()
                  End If
              End If
        End Sub
        'updated code with stop function----------------
Run Code Online (Sandbox Code Playgroud)

我想重置循环并在停止后台工作时将进度条返回到0%.

这可能吗?


上面的代码已经更新,现在工作正常.

我在do循环中添加了这段代码:

        BackgroundWorker1.WorkerSupportsCancellation = True
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True
            ProgressBar1.Value = 0
            Exit Do
        End If
Run Code Online (Sandbox Code Playgroud)

我创建了一个停止工人的按钮:

    Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
          If BackgroundWorker1.IsBusy Then

              If BackgroundWorker1.WorkerSupportsCancellation Then                
                 BackgroundWorker1.CancelAsync()
              End If
          End If
    End Sub
Run Code Online (Sandbox Code Playgroud)

Hes*_*her 10

Backgroundworker类具有CancelAsync()您需要调用以取消执行bgw的方法.

您需要将该Backgroundworker.WorkerSupportsCancellation属性设置为true,并在while循环内部,您需要检查CancellationPending属性,值是否true表示对CancelAsync()方法的调用.

如果CancellationPending计算结果为true,您将(您应该已经完成)调用其中一个重载ReportProgress()(Docu)方法将ProgressBar值设置为所需的值.

编辑:您应该将Cancel属性设置DoWorkEventArgs为true,以便您可以检查事件内部的Cancelled属性.RunWorkerCompletedEventArgsRunworkerCompleted

您也不应该访问UI线程中的任何控件.您最好使用ProgressChanged(Docu)事件.

请参阅:BackgroundWorker Docu