2013-08-26 31 views
5

バックグラウンドワーカーを停止させ、作業中のすべてのプロセスを終了するボタンを作成したいとします。私はループをリセットし、私はBackgroundWorkerのを停止すると0%にプログレスバーを返したいVB.netでバックグラウンドワーカーを停止する

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

は、ここに私のサンプルBackgroundWorkerのコードです。

これは可能ですか?


上記のコードは更新され、正常に動作しています。

は、私は私のDOループ内でこのコードを追加しました:

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

が、私は労働者を停止ボタンを作成:

のBackgroundWorkerクラスは、あなたが呼び出す必要があり方法 CancelAsync()を持って
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

bgwの実行をキャンセルします。

あなたがtrueにBackgroundworker.WorkerSupportsCancellationプロパティを設定する必要があり、whileループ内で、あなたは値がCancelAsync()メソッドの呼び出しを示しtrueで天気をCancellationPendingプロパティをチェックする必要があります。

CancellationPendingが真と評価された場合、あなたは(あなたがすでにやるべき)所望の値にあなたのProgressBar値を設定するために、オーバーロードReportProgress()Docu)のいずれかの方法を呼ぶだろう。

EDIT:あなたはRunworkerCompletedイベント内RunWorkerCompletedEventArgsCancelledプロパティをチェックすることができますので、あなたがtrueにDoWorkEventArgsCancelプロパティを設定する必要があります。

また、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 
関連する問題