I am trying to run a backgroundworker, but only when a certain radiobutton is checked . if it is unchecked then i want to cancel the backgroundworker - the moment when its checkstate changes.
I have tried including the chackstate as an argument of the backgroundworker runworkerAsync call and the put the code in an if statement but itlooks like the thread does not know that the checkstate has changed
Private Sub OptionUpdate_CheckedChanged(sender As Object, e As EventArgs) Handles OptionUpdate.CheckedChanged
If OptionUpdate.Checked = True Then
BackgroundWorker1.RunWorkerAsync(OptionUpdate.Checked)
Else
UpdateOptionBox(False)
End If
End Sub
This is the code for my backgroundworker DoWork method
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim myOptionUpdateState As Boolean
myOptionUpdateState = e.Argument
If BackgroundWorker1.IsBusy Then
If BackgroundWorker1.WorkerSupportsCancellation Then
BackgroundWorker1.CancelAsync()
End If
End If
If myOptionUpdateState = True Then
SetControlTextProperty_ThreadSafe(RadLabel1, "")
Dim myString As String
myString = "Please enter the Fuel charge Rate to update rates..."
For i As Integer = 0 To myString.Length
If myOptionUpdateState = False Then
BackgroundWorker1.CancelAsync()
End If
SetControlTextProperty_ThreadSafe(RadLabel1, myString.Substring(0, i))
Thread.Sleep(120)
If i = myString.Length Then
i = 0
End If
Next
End If
End Sub
How can I make it that when the checkstate changes then the background worker will stop
If you think it you can achieve it