473,383 Members | 1,862 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,383 software developers and data experts.

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 2184
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
1
by: Luis Pinho | last post by:
Hi There, I've got a server that is waiting for requests, these request correspond to calls to objects that are specified in assemblies stored in the GAC. To do this, I use reflection to call...
6
by: Wal Turner | last post by:
Hi there. There are various snippets on forums regarding issues with AppDomain.Unload and how it just doesnt work. Fortunately, I got it working with the base case, after much fiddling. Consider...
13
by: Andy Fish | last post by:
Hi, I am using COM interop to invoke MS word from my .net app. If word pops up a dialog box, my thread obviously hangs until the dialog is answered. Here's the problem: because the app is...
6
by: mflanagan | last post by:
I have unmanaged C++ program that will load a managed C++ dll and then call a function in that dll. The managed C++ routine will call some C# routines. The unmanaged C++ main program will make...
5
by: [Yosi] | last post by:
Why I can't abot a susspended thread. Who can terminat a thread imediatly without consider to its stat or execution?
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...
5
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.