How to name a thread running a Task?

前端 未结 3 1167
暗喜
暗喜 2021-01-03 22:42

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 ac

3条回答
  •  清歌不尽
    2021-01-03 23:24

    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).

提交回复
热议问题