34

Is there a way to specify a Thread's name when using the Task.StartNew method

var task = Task.Factory.StartNew(MyAction, TaskCreationOption.LongRunning, ??ThreadName??);
Jon
  • 38,814
  • 81
  • 233
  • 382

5 Answers5

42

Well, this works:

class Program {
    static void Main(string[] args) {
        var task = Task.Factory.StartNew(() => {
            Thread.CurrentThread.Name = "foo";
            Thread.Sleep(10000);   // Use Debug + Break to see it
        });
        task.Wait();
    }
}

There's a problem however, the threadpool thread gets recycled and won't change its name. This can be confusing, you'll see it running later executing entirely different code. Be sure to take note of this. Your best bet is otherwise to use the Location column in the Debug + Windows + Threads window to find the task back.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Setting the Thread.Name property is only allowed when it's not set already, because Thread.Name is write-once! So a check for null before changing it could be required when the thread is recycled! If you really need to, you could remember the "old" thread name and set it back to what it was before. That's not simple. See [here](https://stackoverflow.com/questions/3353349/thread-renaming) for more about this. – huha Sep 21 '21 at 07:39
20

Not a Thread-name for sure.

Threads and tasks are not 1-to-1 related.

You can use the Task.Id to track it.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 2
    I have a long running task which means a new thread is created and I wanted to name it – Jon Nov 07 '11 at 15:11
  • 1
    No guarantees, just a Hint that could even be ignored. See http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcreationoptions.aspx – H H Nov 07 '11 at 15:22
  • No but I need it definately on another thread and thought I'd read if I specify LongRunning it will do that – Jon Nov 07 '11 at 15:30
  • The default scheduler does actually create a thread with the LongRunning option (in the current, could change in the future, implementation). However, as Henk says a few comments above, _no guarantees_. – Marc Nov 07 '11 at 16:05
  • And that is what I have found but is the general consensus to go back to manually creating a thread and managing it myself? – Jon Nov 07 '11 at 16:15
  • @Jon: "... to manually creating a thread ... " - of course not. – H H Nov 07 '11 at 16:44
  • @Jon: This is de-railing into an entirely different question, un-answerable from the info in your post. Ask a new question. – H H Nov 07 '11 at 16:55
3

I prefer to use Thread.CurrentThread.ManagedThreadId. It's not as good as a name, but does help track the specific work for a thread.

paultechguy
  • 2,318
  • 4
  • 25
  • 34
0

Tasks use the threadpool and a thread can be named only once. What you can do is use a ThreadStatic variable. It will however live between tasks, so set it in every task again.

SijeDeHaan
  • 175
  • 2
  • 2
-2

You could write your own TaskScheduler which sets Thread.CurrentThread.Name when it puts a task on a thread. After the task is done your custom TaskScheduler can also clear Thread.CurrentThread.Name to avoid any confusion.

nitrogenycs
  • 962
  • 9
  • 13
  • 4
    That is not possible actually - once the Name-Property of a Thread has been set it cannot be modified anymore. – ChriPf Oct 16 '14 at 08:44