0

It is my understanding that anything running using .NETs threadpool like this:

ThreadPool.QueueUserWorkItem(myproc);

will get terminated cleanly by the CLR when the application is closed down.

Let's say in this case that the myproc function contains an infinite loop. Is there a way I can hook into the termination of the thread?

4

1 に答える 1

1

No. When the CLR is tearing down your process, it aggressively aborts any remaining background threads. I say aggressively aborts because, unlike a normal call to Thread.Abort(), no exception is raised in your code, so you have no way to respond to it.

At this point, the CLR doesn't particularly care if you have anything left to do; your process is dead, kaput, and about to be buried, and there's nothing you can do to stop it. If something is important enough that it absolutely must be cleaned up before the process terminates, it should go in a foreground thread so that the process will stay around long enough for it to do whatever it needs to do.

于 2012-10-03T18:02:41.113 に答える