Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 702
Nick Avatar asked Jan 25 '10 00:01

Nick


3 Answers

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.

like image 157
REA_ANDREW Avatar answered Nov 12 '22 18:11

REA_ANDREW


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.

like image 28
dtb Avatar answered Nov 12 '22 20:11

dtb


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.

like image 20
Chris O Avatar answered Nov 12 '22 20:11

Chris O