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

2023-09-04 09:41:22 作者:棒棒糖的初恋。

我在想,如果有一个与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.