关于TaskCreationOptions枚举

本文介绍了用于控制任务创建和执行的可选行为标志,包括AttachedToParent、DenyChildAttach、HideScheduler、LongRunning等选项的作用及用法。通过这些标志,开发者能够更好地管理任务之间的依赖关系,以及如何在系统中调度任务。

指定用于控制任务的创建和执行的可选行为的标志。
AttachedToParent:指定将任务附加到任务层次结构中的某个父级。默认情况下,子任务(即由外部任务创建的内部任务)将独立于其父任务执行。可以使用TaskContinuationOptions.AttachedToParent选项以便将父任务和子任务同步。请注意,如果使用DenyChildAttach选项配置父任务,则子任务中的AttachedToParent选项不起作用,并且子任务将作为分离的子任务执行。
DenyChildAttach:指定任何尝试作为附加的子任务执行(即,使用AttachedToParent选项创建)的子任务都无法附加到父任务,会改成作为分离的子任务执行。
HideScheduler:防止环境计划程序被视为已创建任务的当前计划程序。这意味着像StartNew或ContinueWith创建任务的执行操作将被视为Default当前计划程序。
LongRunning:指定任务将是长时间运行的,粗粒度的操作,涉及比细化的系统更少、更强大的组件。它会像TaskScheduler提示,过度订阅可能是合理的。可以通过过度订阅创建比可用硬件线程数更多的线程。它还将提示任务计划程序:该任务需要附加线程,以使任务不阻塞本地线程池队列中其他线程或工作项的向前推动。
None:指定应使用默认行为。
PreferFairness:提示TaskScheduler以一种尽可能公平的方式安排任务,这意味着较早安排的任务将更可能较早运行,而较晚安排运行的任务将更可能较晚运行。
RunContinuationsAsynchronously:强制异步执行添加到当前任务的延续任务。

namespace System.Threading.Tasks { // // 摘要: // Specifies flags that control optional behavior for the creation and execution // of tasks. [Flags] public enum TaskCreationOptions { // // 摘要: // Specifies that the default behavior should be used. None = 0, // // 摘要: // A hint to a System.Threading.Tasks.TaskScheduler to schedule a task in as fair // a manner as possible, meaning that tasks scheduled sooner will be more likely // to be run sooner, and tasks scheduled later will be more likely to be run later. PreferFairness = 1, // // 摘要: // Specifies that a task will be a long-running, coarse-grained operation involving // fewer, larger components than fine-grained systems. It provides a hint to the // System.Threading.Tasks.TaskScheduler that oversubscription may be warranted. // Oversubscription lets you create more threads than the available number of hardware // threads. It also provides a hint to the task scheduler that an additional thread // might be required for the task so that it does not block the forward progress // of other threads or work items on the local thread-pool queue. LongRunning = 2, // // 摘要: // Specifies that a task is attached to a parent in the task hierarchy. By default, // a child task (that is, an inner task created by an outer task) executes independently // of its parent. You can use the System.Threading.Tasks.TaskContinuationOptions.AttachedToParent // option so that the parent and child tasks are synchronized. Note that if a parent // task is configured with the System.Threading.Tasks.TaskCreationOptions.DenyChildAttach // option, the System.Threading.Tasks.TaskCreationOptions.AttachedToParent option // in the child task has no effect, and the child task will execute as a detached // child task. For more information, see Attached and Detached Child Tasks. AttachedToParent = 4, // // 摘要: // Specifies that any child task that attempts to execute as an attached child task // (that is, it is created with the System.Threading.Tasks.TaskCreationOptions.AttachedToParent // option) will not be able to attach to the parent task and will execute instead // as a detached child task. For more information, see Attached and Detached Child // Tasks. DenyChildAttach = 8, // // 摘要: // Prevents the ambient scheduler from being seen as the current scheduler in the // created task. This means that operations like StartNew or ContinueWith that are // performed in the created task will see System.Threading.Tasks.TaskScheduler.Default // as the current scheduler. HideScheduler = 16, // // 摘要: // Forces continuations added to the current task to be executed asynchronously. // Note that the System.Threading.Tasks.TaskCreationOptions.RunContinuationsAsynchronously // member is available in the System.Threading.Tasks.TaskCreationOptions enumeration // starting with the .NET Framework 4.6. RunContinuationsAsynchronously = 64 }这是什么意思
最新发布
10-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值