Naming threads from the thread pool

I was doing some multithreaded programming today and ran into a small problem. While googling for a fix, I found a very interesting item from earlier this year: Be careful naming threads that come from the thread pool (via the asynchronous programming model):

Underneath the hood [...] the CLR rips a thread from from the CLR's thread pool. When the method completes, the thread happily returns to the pool to finish its Pina Colada. Eventually, that thread will resurface, and that's where the problem arises.

You can change the name of a thread, but you can only change it once.If you try to change it again, you're greeted with an InvalidOperationException. When a thread is returned to the thread pool, it holds onto its name. Its happy to have a sense of identity and will hold onto it even when it resurfaces to execute another method.

The original article also posts the workaround.

Published Tuesday, December 7, 2004 5:36 PM by PSteele

Comments

# re: Naming threads from the thread pool

The guy who wrote that article is awesome! ;)

Thursday, December 9, 2004 2:08 AM by Haacked