バックグラウンドワーカーを停止し、作業中のすべてのプロセスを終了できるボタンを作成したいと思います。
これが私のサンプルの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%に戻したい。
これは可能ですか?
上記のコードが更新され、正常に機能しています。
私はこのコードを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
BackgroundworkerクラスにはCancelAsync()
、bgwの実行をキャンセルするために呼び出す必要のあるメソッドがあります。
You need to set the Backgroundworker.WorkerSupportsCancellation
property to true and inside the while loop you need to check the CancellationPending
property wether the value is true
which indicates a call to the CancelAsync()
method.
If CancellationPending
evaluates to true, you would ( which you should have done already ) call one of the overloaded ReportProgress()
(Docu) methods to set your ProgressBar value to the desired value.
EDIT: You should set the Cancel
property of the DoWorkEventArgs
to true so you can check the Cancelled
property of the RunWorkerCompletedEventArgs
inside the RunworkerCompleted
event.
You also shouldn not access any controls which lives in the UI thread. You better use the ProgressChanged
(Docu) event.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加