how to abort a blocked thread?

  • Thread starter Thread starter Leonardo Curros
  • Start date Start date
L

Leonardo Curros

I´d like to know if there is a way to finish a running thread in a
blocking operation.
If i don´t abort thread, what happens with thread when parent
application finish?


Thanks in advance
 
It depends on what you mean by bocked. If its waiting on a synchronization primitive (Monitor, Mutex, Event, etc) the call Interrupt on the thread. If its block in an unmanaged call due to WaitForSingleObject then you have bigger problems. If you are truely taking teh application down then you can call Abort on the thread, but if you are blocked in unmanaged code then this won't help (assuming that the thread is a non-background thread). As with all difficult questions ... it depends.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

I´d like to know if there is a way to finish a running thread in a
blocking operation.
If i don´t abort thread, what happens with thread when parent
application finish?


Thanks in advance
 
Thanks Richard.
I´m trying to do a monitor to a com+ application.The thread does a
call to a com+ object and com+ never answer. When I try to do a
thread.abort, this never finishes, and i have to leave thread up. If i
exit application i supose that thread will continue running.
Really there is no way to abort it?



Richard Blewett said:
It depends on what you mean by bocked. If its waiting on a
synchronization primitive (Monitor, Mutex, Event, etc) the call
Interrupt on the thread. If its block in an unmanaged call due to
WaitForSingleObject then you have bigger problems. If you are truely
taking teh application down then you can call Abort on the thread, but
if you are blocked in unmanaged code then this won't help (assuming
that the thread is a non-background thread). As with all difficult
questions ... it depends.
 
You could try creating a secondary appdomain, create a thread in that
appdomain and call the COM object from there, and if it doesn't respond you
could unload the appdomain - there are no guarantees, but I've observed that
the runtime goes to greater lengths to unload an appdomain then in trying to
abort a single thread from the default appdomain.

If that doesn't do it then your only other options are to live with the hung
thread, or exit the application.
 
how about forcing the thread to be a background thread? that way, you are
guaranteed that cleanup will occur after the main thread terminates.
 
Thanks David.

It seems to be a possible solution to load thread from a secondary
appdomain.
But what happens if i need to launch periodically (inside windows
service) that thread (that will hung) in a sepparated domain and i
unload all this domains?
 
I'm not sure what your question is, but there are several possible outcomes
from launching a secondary appdomain and then trying to unload it...it will
either get unloaded or the unload will hang and an exception will be thrown
back to the default appdomain that it could not unload it (because of the
hung thread).

It seems to me that if a thread has hung in external unmanaged code, then
regardless of what you do in the managed code, the unmanaged code is hosed
up. The problem then is a question of what the proper next step should be -
leave it alone in the hopes that it will fix itself, let the user manually
terminate the process, try to automatically terminate the process and try
again, don't try to access it again, etc. In other words, a hung thread
represents a failure somewhere, and how to recover from that without making
the problem worse is impossible to determine.
 
That does not guarantee that a hung thread can be unwound from unmanaged
code, and it still wont necessarily allow the runtime to unload an
appdomain. When a thread is marked as background, the runtime uses that to
determine when the application should exit - when all foreground threads
have terminated it begins the shutdown process.

Alvin Bruney said:
how about forcing the thread to be a background thread? that way, you are
guaranteed that cleanup will occur after the main thread terminates.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
David Levine said:
You could try creating a secondary appdomain, create a thread in that
appdomain and call the COM object from there, and if it doesn't respond
you could unload the appdomain - there are no guarantees, but I've
observed that the runtime goes to greater lengths to unload an appdomain
then in trying to abort a single thread from the default appdomain.

If that doesn't do it then your only other options are to live with the
hung thread, or exit the application.
 
Back
Top