2013-08-26 173 views
5

我想創建一個按鈕,可以停止我的後臺工作,並結束它正在處理的所有進程。VB.net停止後臺工作

這裏是我的示例代碼的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---------------- 

我想重置循環並返回進度條爲0%時,我停止BackgroundWorker的。

這可能嗎?


上面的代碼已經更新,現在是工作的罰款。

我加入這個代碼,我做循環內:

 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 

回答

9

BackgroundWorker的類有你需要調用的方法CancelAsync()取消bgw的執行。

您需要的Backgroundworker.WorkerSupportsCancellation屬性設置爲true,while循環裏面,你需要檢查CancellationPending屬性羯羊值true指示到CancelAsync()方法的調用。

如果CancellationPending值爲true,你會(你應該已經完成)調用的重載ReportProgress()Docu)方法之一,你的進度值設置爲需要的值。

編輯:您應該DoWorkEventArgsCancel屬性設置爲true,所以你可以檢查RunworkerCompleted事件中的RunWorkerCompletedEventArgsCancelled財產。

你也不應該訪問任何生活在UI線程中的控件。您最好使用ProgressChangedDocu)事件。

參見:BackgroundWorker Docu

+0

非常感謝我已經更新了我的原代碼與工作停止按鈕。我已經在他們之間發表評論說「更新的帶停止功能的代碼」 –

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