Re: C# Threading, and suspending or killing a thread



The thread itself is created by a service process (a web server's
ISAPI that handles authentication). It makes some calls to a dll
(msvcrt.dll) and the info from ProcessExplorer shows it as
msvcrt.dll!endthreadex+0x2f.

Other threads that are spawned like this go right back to zero CPU,
and just go away. But sometimes they get stuck (with 25-50% of the
CPU) and just hang around. I've use the Process Explorer tool to
suspend and to kill the threads, and its had no detrimental effect on
the service. The web service call that generated the issue completes
successfully, and subsequent calls all work fine. That's what I meant
by 'while it was exiting'.

The reason we're looking at this is that these hanging threads can eat
up 100% CPU with just 2 or 4 of these calls hung. Not a perfect
reason to terminate the thread, but, while we're waiting on the
vendor, it beats us having to term serv into the web server and run
the Process Explorer to kill it there.


When you terminate a thread in a .NET application you should consider the whole appdomain doomed, as in, you should no longer keep it around.

While you said it was not a thread in another .NET application, the comment is no less appropriate.

If you terminate a thread from the outside (of the thread, could be in the same process), then you risk the following:

- memory leak, the thread did not get to its cleanup code
- resource leak (open files, sockets, etc.), the thread did not get to is cleanup code
- invalid application state, the thread did not finalize its changes to internal variables and might leave the application in a state that is invalid
- deadlocks, if the thread locks internal resources it will never unlock them, thus blocking future threads from ever accessing the resources the lock protects

Considering this is a service I'd avoid this at all cost. If you absolutely have to kill the thread, can you restart the service in question so that its state is again valid?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse@xxxxxxxxxxx
PGP KeyID: 0x2A42A1C2


.