CancellationTokenSource和出口标志的区别的任务循环退出

编程入门 行业动态 更新时间:2024-10-24 07:32:18
本文介绍了CancellationTokenSource和出口标志的区别的任务循环退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在想,如果有一个与CancellationTokenSource和出口标志的结局循环任务之间的差异

CancellationTokenSource:

CancellationTokenSource cancellationTokenSource; 任务loopTask; 无效StartLoop() {     cancellationTokenSource =新CancellationTokenSource();     loopTask = Task.Factory.StartNew(环,TaskCreationOptions.LongRunning); } 无效循环() {     而(真)     {         如果(cancellationTokenSource.IsCancellationRequested)             打破;         Thread.Yield();     } } 无效StopLoop() {     cancellationTokenSource.Cancel();     loopTask = NULL;     cancellationTokenSource = NULL; }

退出标志:

挥发性布尔exitLoop; 任务loopTask; 无效StartLoop() {     exitLoop = FALSE;     loopTask = Task.Factory.StartNew(环,TaskCreationOptions.LongRunning); } 无效循环() {     而(真)     {         如果(exitLoop)             打破;         Thread.Yield();     } } 无效StopLoop() {     exitLoop = TRUE;     loopTask = NULL; }

对我来说没有任何SANCE使用CancellationTokenSource,顺便说一句是没有任何理由取消标记可以被添加作为参数来工作的工厂?

非常感谢您的任何答案。

最佳ragards teamol

解决方案
  • 使用的CancellationToken 允许令牌来处理所有必要的同步,这样你就不必去想它。
  • 当工作故障是由于在其创作中使用的令牌被标记为取消它集任务的状态来取消,而不是出现故障。如果您使用的是布尔(不要扔)的任务实际上会被标记为成功完成,即使它实际上是取消了。
  • 不像布尔它是引用类型,所以引用了CTS,可以从其他地方传来传和取消(或检查)。这是关键在于这些位置不需要被连接在一起的方式,如果所用的布尔字段他们会;无论是code决定何时操作被取消,也没有任何的code反应的取消,需要知道对方的存在。这允许更大的模块化,抽象,更高层次的功能没有具体的个人情况等。
  • 这增加了增强语义的code。
  • I was wondering if there is any difference between ending loop task with CancellationTokenSource and exit flag

    CancellationTokenSource:

    CancellationTokenSource cancellationTokenSource; Task loopTask; void StartLoop() { cancellationTokenSource = new CancellationTokenSource(); loopTask = Task.Factory.StartNew(Loop, TaskCreationOptions.LongRunning); } void Loop() { while (true) { if (cancellationTokenSource.IsCancellationRequested) break; Thread.Yield(); } } void StopLoop() { cancellationTokenSource.Cancel(); loopTask = null; cancellationTokenSource = null; }

    Exit flag:

    volatile bool exitLoop; Task loopTask; void StartLoop() { exitLoop = false; loopTask = Task.Factory.StartNew(Loop, TaskCreationOptions.LongRunning); } void Loop() { while (true) { if (exitLoop) break; Thread.Yield(); } } void StopLoop() { exitLoop = true; loopTask = null; }

    To me it does not make any sance to use CancellationTokenSource, btw is there any reason why cancellation token can be add as a parameter to Task factory?

    Thank you very much for any kind of answer.

    Best ragards teamol

    解决方案

  • Using a CancellationToken allows the token to handle all necessary synchronization, so you don't have to think about it.
  • When a Task faults due to the token used in its creation being marked as cancelled it sets the state of the Task to cancelled, rather than faulted. If you use a boolean (and don't throw) the task would actually be marked as completed successfully, even though it was actually cancelled.
  • Unlike a boolean it's a reference type, so the reference to the CTS can be passed around and cancelled (or inspected) from other locations. This is key in that these locations don't need to be coupled together the way that they would if you used a boolean field; neither the code deciding when the operation is cancelled, nor any of the code reacting to the cancellation, need to know about each other. This allows for greater modularization, abstraction, higher levels of functionality not specific to individual circumstances, etc.
  • It adds enhanced semantic meaning to the code.
  • 更多推荐

    CancellationTokenSource和出口标志的区别的任务循环退出

    本文发布于:2023-11-08 07:00:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1568666.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:标志   区别   CancellationTokenSource

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!