Anushya said:
Hi
When i abort a thread i get threadabort exception and wot all the
things that need to be done there? IS releasing all the objects that
are created is necesssary before aborting?Or aborting alone is
sufficient?
It sounds like you are confusing the roles of a number of different
mechanisms. Aborting a thread injects an abort exception into the target
thread. If the abort is injected into the thread from a different thread
then it is injected asynchronously and it can interrupt a number of
execution paths, such as finally blocks and static constructors, that you
really don't want interrupted. If it is thrown by itself then it is
synchronous and so long as you understand the consequences it should not
cause harmful side-effects.
The abort causes an exception to be raised, and even if a catch block handle
the exception, once the catch block body is exited the exception will be
rethrown so the next higher catch block sees it, until all catch (and
finally) blocks have run. Eventually the thread returns from its start
method and the thread terminates.
By itself an abort does not clean up any resources nor does it call Dispose
on objects. All it does is execute catch and finally blocks until the thread
exits or the abort is reset. If you need cleanup logic then you must write
code for it.
Aborting a thread is usually not a good thing to do as it has side-effects
that can be harmful - do not use it unless you know that you wont get bit by
the side-effects. There are other methods you can use to cause a thread to
terminate, such as setting a field, signalling an event, etc. that the
thread periodically checks and if set causes it to return from its thread
start method. Also, if the thread is executing unmanaged code the abort will
not take affect until the thread returns from the unmanaged code and begins
executing managed code again.
Join ing the thread till it is killed is impossible in my scenario as
the exception is routed to some other layer. In the layer where abort
is done, the user will not handle it..
This does not make sense. Does the other layer control the thread? A
different design would be preferable to that. What would the user do to
handle an abort exception?