473,239 Members | 1,549 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,239 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 2306
<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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.