Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.net stopping a backgroundworker
    primarykey
    data
    text
    <p>I want to create a button that could stop my background worker and end all the process it is working on.</p> <p>Here is my sample backgroundworker code:</p> <pre><code> Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try If BackgroundWorker1.IsBusy &lt;&gt; 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 &lt;= 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---------------- </code></pre> <p>I want to reset the loop and return the Progress Bar to 0% when i stop the backgroundworker.</p> <p>Is this possible?</p> <hr> <p>The code above has been updated and it is now working fine. </p> <p>I have added this code inside my do loop:</p> <pre><code> BackgroundWorker1.WorkerSupportsCancellation = True If BackgroundWorker1.CancellationPending Then e.Cancel = True ProgressBar1.Value = 0 Exit Do End If </code></pre> <p>I created a button that stops the worker:</p> <pre><code> 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload