【问题标题】:VB.net stopping a backgroundworkerVB.net 停止后台工作者
【发布时间】:2013-08-26 05:37:02
【问题描述】:

我想创建一个按钮来停止我的后台工作人员并结束它正在处理的所有进程。

这是我的后台工作程序示例代码:

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

当我停止后台工作程序时,我想重置循环并将进度条返回到 0%。

这可能吗?


上面的代码已经更新,现在可以正常工作了。

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

        BackgroundWorker1.WorkerSupportsCancellation = True
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True
            ProgressBar1.Value = 0
            Exit Do
        End If

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

    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

【问题讨论】:

    标签: vb.net backgroundworker


    【解决方案1】:

    Backgroundworker 类有 CancelAsync() 方法,您需要调用该方法来取消 bgw 的执行。

    您需要将Backgroundworker.WorkerSupportsCancellation 属性设置为true,并且在while 循环中您需要检查CancellationPending 属性的值是否为true,这表示调用了CancelAsync() 方法。

    如果CancellationPending 计算结果为真,您将(您应该已经完成​​的操作)调用重载的ReportProgress() (Docu) 方法之一来将您的 ProgressBar 值设置为所需价值。

    编辑:您应该将DoWorkEventArgsCancel 属性设置为true,这样您就可以在RunworkerCompletedevent 中检查RunWorkerCompletedEventArgsCancelled 属性。

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

    见:BackgroundWorker Docu

    【讨论】:

    • 非常感谢,我已经用工作停止按钮更新了我的原始代码。我在他们之间发表了一条评论,说“更新了带有停止功能的代码”
    【解决方案2】:
    Public Class Form1
        Private iVal As Integer = 0
        Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
            For iVal = iVal To 100 Step 1
                bgw.ReportProgress(iVal)
                Threading.Thread.Sleep(99)
                If (bgw.CancellationPending = True) Then
                    e.Cancel = True
                    Exit For
                End If
            Next
        End Sub
    
        Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
            pbar.Value = e.ProgressPercentage
            lblProgrss.Text = e.ProgressPercentage.ToString() & "%"
        End Sub
    
        Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
    
            If (e.Cancelled = True) Then
                pic.Visible = False
                pbar.Value = iVal
                lblProgrss.Text = iVal & "%"
                btnstart.Text = "Start"
                btnstart.BackColor = Color.Green
            Else
                pic.Visible = False
                btnstart.Text = "Start"
                btnstart.BackColor = Color.Green
                iVal = 0
            End If
    
        End Sub
    
        Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
            If (btnstart.Text = "Start") Then
                btnstart.Text = "Stop"
                btnstart.BackColor = Color.Red
                pic.Visible = True
                bgw.RunWorkerAsync()
            Else
                If (bgw.IsBusy = True) Then
                    btnstart.Text = "Start"
                    btnstart.BackColor = Color.Green
                    bgw.CancelAsync()
                End If
            End If
        End Sub
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多