6

I use Tasks to run asynchronous operations in .NET.

In order to make a better use of the Threads debugging window, I would like to identify the currently running task by a thread name. However, the Name property of each thread can only be set once. So the second time the same thread from the pool is used, I'm getting a runtime exception.

Is there any way to get around this?

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • 4
    This isn't a direct answer to your question, but have you looked at the `Debug -> Windows -> Parallel Tasks` window? It shows (among other things): the status of a task, the `Id` of the thread its running on, and its target method. – Ani May 08 '11 at 08:39
  • 3
    I wonder why the name is limited to being set once? I could understand having a name that had to be uniquely set before a thread was started (with the system perhaps generating a GUID if nothing else was specified), but even if one wanted each thread to have an immutable name identifying it, it would be useful to have a mutable "description" field. Any idea why thread names/descriptions aren't mutable? – supercat Mar 23 '12 at 16:15

4 Answers4

4

When you schedule your tasks with the TaskCreationOptions.LongRunning, it runs on its own thread, not a thread from the ThreadPool, so you can safely set the thread's name.

Using this method will probably have performance impact, so I would suggest using it only during debugging. Also, it seems the number of threads created this way isn't limited (apart from the global limits on number of threads), so it won't behave the same as without using this option.

svick
  • 236,525
  • 50
  • 385
  • 514
2

Is there any way to get around this?

Well, technically yes. I mean you could use reflection to set the name. See my answer here for how to do it. Naturally, I do not recommend setting private fields via reflection for production code, but I suppose you can get away with it since you only want to use it for debugging.

Community
  • 1
  • 1
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
1

As Ani notes in his comment: Parallel Tasks Window will give you details of the state of all existing Tasks. This, and the Parallel Stacks, tools window will give better detail for most debugging of tasks.

Is it possible to give a temporary name to a thread from the thread pool?

Thread.Name allows you to set a thread's name, but (if I recall correctly) this can only be set once for a single thread rather limiting its usefulness for thread pool threads (tasks run on thread pool threads).

Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
0

You can always name the thread in the moment that the thread start with Thread.CurrentThread.Name.

//this is how you put the thread in the ThreadPool.
ThreadPool.QueueUserWorkItem(new WaitCallback(this.Handler), this);  


//this is how you name the Thread
void Handler(object parameters)
{
    Thread.CurrentThread.Name = "MyThreadName";
    while(true)
    {

    }
}
OscarSanhueza
  • 317
  • 2
  • 13
  • 1
    The Name property is WriteOnce, so if the name was already set (by some other task running on ThreadPool threads?) it will throw an exception. – Sam Apr 03 '14 at 08:10