如何在C#中获取TPL任务线程的引用? [英] How to get the reference of TPL task's thread in C#?

查看:58
本文介绍了如何在C#中获取TPL任务线程的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以

Task task = Task.Factory.StartNew(() => someMethod(args));

在C#4.0+中,如何获得此任务的线程的引用?

in C# 4.0+, how can I get the reference of the thread(s) of this task?

是否有可能在创建任务的同一线程中执行任务或产生多个线程?

Is it possible that the task is executed in the same thread that created the task or spawn more than one thread?

更新:
原因是:

Update:
The reasons are:

  • 我想在调试器中标识任务的线程(并为其命名属性),等等.

创建的任务是否总是在与创建任务的线程不同的线程中执行?
是一个,零个或多个线程?
它是在同一个内核上执行的吗?
知道这一点很重要,因为例如,我可以以冻结我的后台工作人员的身份进入主线程

Is created task executed always in separate thread from the one in which a task was created?
Is it one, zero or more than one thread?
Is it executed on a single and the same core?
It is important to know since, for example, I can put to sleep the main thread thinking that I am freezing the background worker

更新:
有用的答案:

Update:
Useful answer:

推荐答案

创建的任务是否总是在与创建任务的线程不同的线程中执行?

Is created task executed always in separate thread from the one in which a task was created?

否,在某些情况下,TPL能够确定可以在创建任务的同一线程上执行任务,这是因为提供了相关的任务创建选项(或任务计划程序),或者是作为优化因为调用线程否则将无事可做.但是,您实际上不必担心此事.这并不是说您最终将阻塞UI线程,因为TPL选择在那种情况下执行它的代码.除非您明确指出,否则不会发生这种情况.出于所有意图和目的,您都可以假设这种情况永远不会发生(除非您强迫它发生),但是在幕后,无需您意识到它,是的,它会发生.

No, there are certain situations in which the TPL is able to determine that the task can be executed on the same thread that created it, either because the relevant task creation option (or task scheduler) was supplied, or as an optimization because the calling thread would otherwise not have anything to do. You don't really need to worry about this though; it's not like you're going to end up blocking the UI thread because the TPL choose to execute it's code in that context. That won't happen unless you specifically indicate that it should. For all intents and purposes you can assume that this never happens (unless you force it to happen) but behind the scenes, without you ever needing to realize it, yes, it can happen.

是一个,零个或多个线程吗?

Is it one, zero or more than one thread?

默认情况下,任务在线程池中执行.线程池将根据给定的工作负载而改变其包含的线程数.它从一开始就开始,但是如果有足够的需求就会增长,而如果这种需求消失了就会收缩.如果指定LongRunning选项,将仅为此Task创建一个新线程.如果您指定一个自定义TaskScheduler,则可以使其随心所欲.

By default, tasks are executed in the thread pool. The thread pool will vary in the number of threads it contains based on the workload it's given. It will start out at one, but grow if there is sufficient need, and shrink if that need disappears. If you specify the LongRunning option, a new thread will be created just for that Task. If you specify a custom TaskScheduler, you can have it do whatever you want it to.

它是在同一个内核上执行的吗?

Is it executed on a single and the same core?

可能,但不能确定.

知道这一点很重要,因为例如,我可以以冻结我的后台工作人员的身份进入主线程

It is important to know since, for example, I can put to sleep the main thread thinking that I am freezing the background worker

将主线程置于睡眠状态将不会阻止后台工作人员正常工作.这就是创建后台工作程序的全部要点,这两个任务不会彼此停止工作.请注意,如果后台工作人员曾经尝试访问UI来报告进度或显示结果,并且UI被阻止,那么他们将在此时等待UI线程空闲.

Putting the main thread to sleep will not prevent background workers from working. That's the whole point of creating the background workers, the two tasks don't stop each other from doing work. Note that if the background workers ever try to access the UI either to report progress or display results, and the UI is blocked, then they will be waiting for the UI thread to be free at that point.

这篇关于如何在C#中获取TPL任务线程的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆