470,593 Members | 2,402 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to manually abort a thread on unload a domain?

When I use the method Thread.Abort, it only sends a request of aborting to OS
to stop a thread. The thread itself isn't killed and allocated resources
aren't released. I tried to run that thread inside of a domain, specially
created for it, but the domain can't be unloaded, 'till there are some not-
stopped threads.

Please, tell me are there any ways to immediately stop a thread or unload a
domain?

Thanks beforehand!
Apr 6 '06 #1
2 1998
> The thread itself isn't killed and allocated resources aren't released.
...
Please, tell me are there any ways to immediately stop a thread or unload a
domain?


If you want to stop the threads because the app is stopping, then you can
set the thread's IsBackground=true. When the app stops, the threads will
stop abruptly. If your threads can stand that (eg a partially updated
file?), then all is well.

If you want the app to continue but have the threads stop and clean up their
resources, then IMO there is no good built in way. I am sure others will
disagree, but at least in .net 2003, I will not be persuaded. I have had
some nasty experiences in this area, and O'Reilly in ".NET Gotchas" has an
entire chapter devoted to threading issues.

I do this function with the following home grown mechanism. I have a
program wide shared integer variable that is thread protected by the
Threading.Interlocked functions. Threads get a copy of this variable when
they start. When I want to cancel threads, I increment the variable.
Threads periodically compare their copy with the global, and they quit
gracefully when they are different. The main advantage of this design (over
say a global boolean) is that while some threads are cancelling, others can
be started, and there is no interference.
Apr 6 '06 #2
Thread.Abort is delivered to the thread when it is executing managed code.
If the thread makes a call to unmanaged code then the delivery of the
Thread.Abort is delayed until the thread returns to managed code. For v1.1
if a thread is "stuck" in unmanaged code then even unloading an appdomain
will fail as the runtime will not be able to unwind the thread from
unmanaged code - the unload will timeout and the appdomain will remain
loaded in memory and you will get an AppdomainUnloadException. I haven't
tried this yet in v2.0 so I can't verify if it behaves differently under
2.0.

When the application itself is being shutdown all threads are suspended;
the shutdown procedure does not attempt to unwind the threads so the
shutdown will succeed even if a thread is "stuck" somewhere. During shutdown
it does not matter if a thread is marked as background or not.

In the 1.1 runtime if you have an app that must call out to unmanaged code
and if you cannot be sure that the unmanaged code will always return from
the call then your options are limited. About the only surefire way to
ensure that the resources get released is to actually exit the application.
There have been some improvements to this in the 2.0 runtime but I haven't
worked with that version enough yet to know its limitations. There is a
concept called an appdomain rude unload which may provide the ability to
unload an appdomain even if a thread remains stuck but I am unsure of
exactly what its capabilities are; I need to do some reading on this.
"Artem" <u20565@uwe> wrote in message news:5e5e074e0efc0@uwe...
When I use the method Thread.Abort, it only sends a request of aborting to
OS
to stop a thread. The thread itself isn't killed and allocated resources
aren't released. I tried to run that thread inside of a domain, specially
created for it, but the domain can't be unloaded, 'till there are some
not-
stopped threads.

Please, tell me are there any ways to immediately stop a thread or unload
a
domain?

Thanks beforehand!

Apr 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Luis Pinho | last post: by
6 posts views Thread by Wal Turner | last post: by
13 posts views Thread by Andy Fish | last post: by
6 posts views Thread by mflanagan | last post: by
5 posts views Thread by [Yosi] | last post: by
18 posts views Thread by Urs Vogel | last post: by
5 posts views Thread by Alan T | last post: by
6 posts views Thread by mehdi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.