Nick
Nick

Reputation: 7525

Name of the thread in the ThreadPool - C#

I am using ThreadPool to execute a set of tasks in a windows service. The service spawns new threads every 10seconds. I would like to record the name of the thread that picked up a particular task from the database. Is it possible to get the name of the thread?

Upvotes: 10

Views: 7810

Answers (4)

Gowtham Bc
Gowtham Bc

Reputation: 1

If you are using thread pool, please use

using system.Threading;

Threading.Thread.CurrentThread.Name

Upvotes: 0

Chris O
Chris O

Reputation: 5037

Don't try and change thread state when using threads from the pool. These threads don't belong to you, they belong to the runtime. You don't want to be changing stuff you don't own.

Upvotes: 1

REA_ANDREW
REA_ANDREW

Reputation: 10774

The easiest way for this will be from inside the context of each thread as you have the CurrentThread property available and all the properties that are attributed to a thread:

System.Threading.Thread.CurrentThread.Name

Like a previous poster has mentioned though, how meaningful this will be from with in a thread pool, I am not sure.

Upvotes: 9

dtb
dtb

Reputation: 217351

I don't know if ThreadPool threads are assigned a meaningful Name, but you should always be able to use the ManagedThreadId of the CurrentThread for debugging/logging purposes.

Upvotes: 7

Related Questions