views:

246

answers:

3

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?

+3  A: 

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.

dtb
Thanks. Is there any value in storing the ManagedThreadId. How would I realistically use it in debugging?
Well, it's an `int` that is the same whenever two UserWorkItems are executed on the same ThreadPool thread. The value has only meaning during one program run. I doubt there is any value in logging ThreadPool thread names or IDs. It's an implementation detail which UserWorkItem is executed on which thread. You shouldn't really care about this. UserWorkItem should not influence each other if they are executed on the same thread.
dtb
+3  A: 

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.

REA_ANDREW
A: 

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.

Chris O