468,140 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Immediate Termination of a BackgroundWorker Thread

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
Feb 28 '07 #1
5 10475
On Feb 27, 7:37 pm, redear <red...@nowhere.com.invalidwrote:
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

Feb 28 '07 #2
redear wrote:
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.

Feb 28 '07 #3
On 28 Feb 2007 05:23:47 -0800, "Branco Medeiros"
<br*************@gmail.comwrote:
>redear wrote:
>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.
Branco,

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

redear
Feb 28 '07 #4
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

redear wrote:
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
Feb 28 '07 #5
On Wed, 28 Feb 2007 13:08:38 -0500, "TP"
<tp***************@mailandnews.comwrote:
>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
Feb 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by dm1608 | last post: by
2 posts views Thread by Sebastian Crewe | last post: by
9 posts views Thread by =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post: by
8 posts views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
2 posts views Thread by =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.