Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name a thread running a Task?

I find naming threads to be very useful when debugging.

I can see no way to name a thread using arguments to Task.Factory.StartNew()

So is it acceptable to name the thread explicitly in the task? eg:

private void MyFunc()
{
    Task.Factory.StartNew(() =>
    {
        Thread.CurrentThread.Name = "Foobulizer";
        Foobulize();
    });
}

However, I appreciate that threads may be reused for different tasks, so would I need to explicitly reset the thread name at the end of the task? This feels pretty hacky so I'm thinking this is probably a bad idea, or there's a proper way to do it?

like image 388
GazTheDestroyer Avatar asked Feb 23 '12 10:02

GazTheDestroyer


People also ask

How do you find the thread name in run?

In the run() method, we use the currentThread(). getName() method to get the name of the current thread that has invoked the run() method. We use the currentThread(). getId() method to get the id of the current thread that has invoked the run() method.

How do you name a thread in C#?

In C#, a user is allowed to assign a name to the thread and also find the name of the current working thread by using the Thread.Name property of the Thread class. Here, the string contains the name of the thread or null if no name was assigned or set.

Can we set thread name after start?

Yes, we can change the name of the main thread. It can be done by first getting the reference of the main thread by using currentThread() method and then calling setName() method on it.


2 Answers

You cannot do this since multiple Tasks can share the same or multiple threads depending on ThreadPool state in a given moment of time. Basically, Task is not a Thread. It's just a high level abstraction for the asynchronous operation. You can use the Task.Id property, but keep in mind that Id is readonly and of int type, so you can't assign custom user-friendly name.

Task IDs are assigned on-demand and do not necessarily represent the order in the which Task instances were created

Take a look at the built-in Visual Studio 2010 Parallel Debugging features. Perhaps you'll find another approach: Walkthrough: Debugging a Parallel Application

Parallel Tasks Window:

enter image description here

For each running Task, you can read its ID, which is returned by the same-named property, the ID and name of the thread that runs it, its location (hovering over that displays a tooltip that has the whole call stack). Also, under the Task column, you can see the method that was passed into the task; in other words, the starting point

like image 104
sll Avatar answered Oct 06 '22 09:10

sll


The default TaskScheduler uses the .NET ThreadPool to schedule the tasks. So you will get a thread that already exists or one that will be reused (both potentially, but likely).

Note that you can only name a thread once. The second attempt to call Thread.CurrentThread.Name will raise an InvalidOperationException. This is particularly bad when it is a thread pool thread.

In general you should not change attributes of a thread that you did not (explicitly) create or own (the name being one, the priority being the other prominent candidates).

like image 40
Christian.K Avatar answered Oct 06 '22 11:10

Christian.K