views:

82

answers:

2

System.Threading.Thread.Name is a write-once property. I can assign to it at once, and only then if no other assignment has been made by application or library code. I am writing a multi-threaded event-driven application and would very much like to be able to change the name of the currently executing thread depending on the task its performing at the time.

Does anyone know why this has to be so, or is this a (wrong) presumption on the part of the CLR's designers?

+2  A: 

This seems to be by design, my personal guess is that the internal InformThreadNameChangeEx shall be called only once per thread.

You could, however, use a threadstatic field to store the name of the currently running thread and then use this field (for instance for logging).

Lucero
+1  A: 

I found this old blog post saying that it's for consistency.

In my opinion it should be changed back to be settable as much as we like, for the exact scenario you described. When using the thread pool I'll be glad to have a name set for the current task to differentiate easily between those ten paused threads in the debugger.

Julien Lebosquain
Inside your thread pool you could keep a dictionary mapping the initial name or the thread id to some custom name as a workaround.
0xA3
Yes I know but once I'm looking at the dictionary and mapping name it defeats the "see quickly in the Threads window which thread is doing what", since that's the only real use of the thread name for me: for debugging at dev time. For logging purpose, of course I already use other means. Thanks for your comments though.
Julien Lebosquain