VB .Net 2.0 question.
I'm using the BackgroundWorker Component on a form. Everything works great, except for updating the status strup progress bar and canceling all work in the background worker thread. Basically, I want to kill all thread operations when the user clicks a cancel button. Accouding to the API, the best that the BackgroundWorker has is a RequestCancel which then flips a boolean (CancellationPending) to true. It is then up to the user to determine what action to take. From hunting the web, the best I can tell is that the intended action is to check if the thread has been cancelled and then ignore updates and let the thread terminate on it's own. Something like this:
While that's fine and dandy for something like a database query where you're sucking CPU time on somebody else's computer, I'm doing extremely intense operations on my machine. If I press cancel, it's becuase I need the operations to cease, not stop showing me the updates. If the thread keeps hogging CPU cycles then I can't get work done and I certianly can't fix whatever was wrong and then crank up a new thread. I'm pretty sure that if I throw an unhandled exception in the worker thread then the thread will instantly terminate. That sounds really hacky and I'm not too confident that it will actually get everything to stop.
What is the best way to kill a BackgroundWorker immediatly?
I'm using the BackgroundWorker Component on a form. Everything works great, except for updating the status strup progress bar and canceling all work in the background worker thread. Basically, I want to kill all thread operations when the user clicks a cancel button. Accouding to the API, the best that the BackgroundWorker has is a RequestCancel which then flips a boolean (CancellationPending) to true. It is then up to the user to determine what action to take. From hunting the web, the best I can tell is that the intended action is to check if the thread has been cancelled and then ignore updates and let the thread terminate on it's own. Something like this:
Visual Basic:
Private Sub ExecutePlugins(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _worker.DoWork
'
' Kick off Time consuming process
'
End Sub
'
Private Sub ProcessUpdate(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles _worker.ProgressChanged
If Not Me._worker.CancellationPending Then
'
' update the status for the user
'
End If
End Sub
'
Private Sub ExecutionCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _worker.RunWorkerCompleted
'
' Clean everything up becuase you're done.
'
End Sub
What is the best way to kill a BackgroundWorker immediatly?