Immediate Termination of a BackgroundWorker Thread

R

redear

Is there a way to immediately terminate a BackgroundWorker thread? My
problem is that the BackgroundWorker starts with a call to
My.Computer.FileSystem.GetFiles that can run for a very long time if
it is pointing to a directory tree with many files. If the user
requests cancellation during this time, the main thread can call
CancelAsync and can post a "Cancellation Pending" message to the user,
but the BackgroundWorker cannot respond to the CancellationPending
property until the call to My.Computer.FileSystem.GetFiles returns. It
seems inelegant to rewrite the call to My.Computer.FileSystem.GetFiles
to use calls to Dir or something that would allow me to poll the
CancellationPending property while I'm building the collection of file
names. And I have not wanted to rewrite the BackgroundWorker as a
standard thread (read that "go through the learning curve"!). I would
rather abort the BackgroundWorker thread but do not see how to do it.

Thanks.

redear
 
R

rowe_newsgroups

Is there a way to immediately terminate a BackgroundWorker thread? My
problem is that the BackgroundWorker starts with a call to
My.Computer.FileSystem.GetFiles that can run for a very long time if
it is pointing to a directory tree with many files. If the user
requests cancellation during this time, the main thread can call
CancelAsync and can post a "Cancellation Pending" message to the user,
but the BackgroundWorker cannot respond to the CancellationPending
property until the call to My.Computer.FileSystem.GetFiles returns. It
seems inelegant to rewrite the call to My.Computer.FileSystem.GetFiles
to use calls to Dir or something that would allow me to poll the
CancellationPending property while I'm building the collection of file
names. And I have not wanted to rewrite the BackgroundWorker as a
standard thread (read that "go through the learning curve"!). I would
rather abort the BackgroundWorker thread but do not see how to do it.

Thanks.

redear


If it's running in a seperate background thread, why do you need it to
terminate immediately? After all what the user doesn't see can't hurt
them (well, sorta :) )

Thanks,

Seth Rowe
 
B

Branco Medeiros

redear said:
Is there a way to immediately terminate a BackgroundWorker thread? My
problem is that the BackgroundWorker starts with a call to
My.Computer.FileSystem.GetFiles that can run for a very long time if
it is pointing to a directory tree with many files. If the user
requests cancellation during this time, the main thread can call
CancelAsync and can post a "Cancellation Pending" message to the user,
but the BackgroundWorker cannot respond to the CancellationPending
property until the call to My.Computer.FileSystem.GetFiles returns.
<snip>

You may try using Thread.Abort, which will cause an exception to be
thrown in the thread that you want to finish. You must call
Thread.ResetAbort in you Catch or Finally block, or else the exception
will bubble up...

A few words of caution, though: thread synchronization is almost an
art, and maybe you start getting more troubles than the one you want
to solve, such as bad breath, too much sugar in your coffee or strange
calls to your cell phone at 3 a.m. ...

<code>
'At file level:
Imports SysThread = System.Threading
Imports SysComps = System.ComponentModel

'...
'At form level:
Private mThread As SysThread.Thread

Private Sub BGWorker_DoWork( _
ByVal Sender As Object, _
ByVal E As SysComps.DoWorkEventArgs _
) Handles BGWorker.DoWork

mThread = SysThread.Thread.CurrentThread
Try
For X As Integer = 0 To 100
SysThread.Thread.Sleep(1000)
BGWorker.ReportProgress(X)
Next
Catch Ex As SysThread.ThreadAbortException
SysThread.Thread.ResetAbort()
End Try
End Sub

Private Sub BTNCancel_Click( _
ByVal Sender As Object, _
ByVal E As System.EventArgs _
) Handles BTNCancel.Click

If BGWorker.IsBusy AndAlso _
mThread IsNot Nothing Then
mThread.Abort()
mThread = Nothing
End If
End Sub
</code>


In the code above, BGWorker is the background worker component, and
BTNCancel is, as you may have guessed, a "cancel" button.

HTH.

Regards,

Branco.
 
R

redear

<snip>

You may try using Thread.Abort, which will cause an exception to be
thrown in the thread that you want to finish. You must call
Thread.ResetAbort in you Catch or Finally block, or else the exception
will bubble up...

A few words of caution, though: thread synchronization is almost an
art, and maybe you start getting more troubles than the one you want
to solve, such as bad breath, too much sugar in your coffee or strange
calls to your cell phone at 3 a.m. ...

<code>
'At file level:
Imports SysThread = System.Threading
Imports SysComps = System.ComponentModel

'...
'At form level:
Private mThread As SysThread.Thread

Private Sub BGWorker_DoWork( _
ByVal Sender As Object, _
ByVal E As SysComps.DoWorkEventArgs _
) Handles BGWorker.DoWork

mThread = SysThread.Thread.CurrentThread
Try
For X As Integer = 0 To 100
SysThread.Thread.Sleep(1000)
BGWorker.ReportProgress(X)
Next
Catch Ex As SysThread.ThreadAbortException
SysThread.Thread.ResetAbort()
End Try
End Sub

Private Sub BTNCancel_Click( _
ByVal Sender As Object, _
ByVal E As System.EventArgs _
) Handles BTNCancel.Click

If BGWorker.IsBusy AndAlso _
mThread IsNot Nothing Then
mThread.Abort()
mThread = Nothing
End If
End Sub
</code>


In the code above, BGWorker is the background worker component, and
BTNCancel is, as you may have guessed, a "cancel" button.

HTH.

Regards,

Branco.

Branco,

Thanks for the clear example as well as the words of warning. Very
helpful!

redear
 
T

TP

I recommend you use FindFirstFile/FindNextFile/FindClose via
pinvoke. That way you could periodically check for a pending
cancel and then exit.

For example, execute FindNextFile in a loop for 100 iterations,
and then check for a pending cancel. If there is a pending cancel
do a FindClose and exit the loop.

-TP
 
R

redear

I recommend you use FindFirstFile/FindNextFile/FindClose via
pinvoke. That way you could periodically check for a pending
cancel and then exit.

For example, execute FindNextFile in a loop for 100 iterations,
and then check for a pending cancel. If there is a pending cancel
do a FindClose and exit the loop.

-TP

TP,

Thanks. That's what I am going to do. Looks like it is better practice
than aborting a thread from the thread pool, even if it is a few more
statements.

Thanks.

redear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top