473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Thread.Abort and Finally block question

Greetings, all! I've been pondering the pitfalls of aborting a
secondary thread in a service app I'm writing (VB, fx v1.1).
Everything I've read so far pretty much dissuades one from aborting one
thread from another, and I'm almost at the point of acquiescing but
curiosity leads me on.

Below is a procedure template that I wonder might guarantee behavior
during an abort, and I'd like to get some feedback in case it's just
plain nonsense.

Private Sub WorkerThread()
Dim finallyDone As Boolean = false
Try
'
' do some stuff
'
Catch ex As Exception
'
' do something exceptional
'
Finally
'
' Question: Would the following loop insure
' completion of the Finally block even
' if an abort exception occurred during
' execution of the block?
'
Do Until finallyDone
Try
'
' do work that *has* to complete, even if it
' means taking more than one run at it
'
finallyDone = True
Catch ex As Exception
'
' do something exceptional, like
' Thread.ResetAbort()
'
End Try
Loop
End Try
End Sub

In my particular situation, I want to raise an event from the Finally
block which passes data from the secondary thread back to the main
thread just before the secondary thread ends, something like this:

Do Until finallyDone
Try
RaiseEvent WorkerThreadEnd(WorkResults)
finallyDone = True
Catch ex As Exception
'
' do something exceptional, like
' Thread.ResetAbort()
'
End Try
Loop

The concern over the possibility of an abort stems from the fact that
the main thread keeps tabs on the time taken by the secondary thread.
If the main thread thinks the secondary thread has timed out, it issues
the Abort request on the secondary thread. No doubt there would come a
time when the secondary thread is one line away from finishing but a
few timeslices beyond the main thread's timeout period. In essence I'm
trying to sidestep the abort if the secondary thread is just moments
from completing, i.e., we just stepped into the Finally block.

Any takers?

TIA, Paul

Jul 21 '05 #1
2 2315
<co**********@yahoo.com> wrote:
Greetings, all! I've been pondering the pitfalls of aborting a
secondary thread in a service app I'm writing (VB, fx v1.1).
Everything I've read so far pretty much dissuades one from aborting one
thread from another, and I'm almost at the point of acquiescing but
curiosity leads me on.


<snip>

The Finally block will be executed if the AbortException occurs during
the Try block. However, if the AbortException occurs during the Finally
block, the block will be aborted abruptly (i.e. without finishing the
Finally block).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Thanks for the quick response, Jon. That's what I was afraid of but
makes sense. So much for Thread.Abort then!

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Bill | last post by:
Say I have a childThread currently is running a finally block to cleanup external resources. At the same time the main thread calls childThread.Abort(). The question is: when the...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
4
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
2
by: cottonviking | last post by:
Greetings, all! I've been pondering the pitfalls of aborting a secondary thread in a service app I'm writing (VB, fx v1.1). Everything I've read so far pretty much dissuades one from aborting one...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
7
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my Windows application, I'm using thread, and I've 2 questions: 1. I read that it's not recommended to use Thread.Abort(), so what is the best way to close the thread, in case the user...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.