gpt4 book ai didi

c# - 使用 ContinueWith 自继续任务

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:08 25 4
gpt4 key购买 nike

我有一个任务需要定期运行。我的第一个实现是这样的:

public static void CheckTask(CancellationTokenSource tokenSource)
{
do
{
// Do some processing
Console.WriteLine("Processing");

// Sleep awhile and wait for cancellation
// If not cancelled, repeat
} while (!tokenSource.Token.WaitHandle.WaitOne(1500));

Console.WriteLine("Bye bye");
}

这个任务是这样开始的:

CancellationTokenSource tokenSource = new CancellationTokenSource();
Task task = null;
task = new Task((x)=> {
CheckTask(tokenSource);
//CheckTask2(t, (object)tokenSource);
}, tokenSource.Token);
task.Start();

然后我想与其在任务中循环,不如使用 ContinueWith 重新安排它?我的下一个实现是这样的:

public static void CheckTask2(Task task, object objParam)
{
CancellationTokenSource tokenSource = (CancellationTokenSource)objParam;
// Do some processing
Console.WriteLine("Processing");
// Sleep awhile and wait for cancellation
if(tokenSource.Token.WaitHandle.WaitOne(1500))
{
Console.WriteLine("Cancel requested");
return;
}
// Reschedule
task.ContinueWith(CheckTask2, tokenSource);
}

第二个实现更容易阅读和编写,我的测试没有显示任何区别,但我仍然想知道 ContinueWith 本身是否有缺点?

最佳答案

I still wonder if there are drawbacks for a task to ContinueWith itself?

坦率地说,我发现你的代码在附加延续的情况下可读性较差(但这只是基于 flavor )。我看到的唯一缺点是您在 token 上使用了 WaitHandle,这迫使您现在转到 dispose your CancellationToken object。 :

Accessing this property causes a WaitHandle to be instantiated. It is preferable to only use this property when necessary, and to then dispose the associated CancellationTokenSource instance at the earliest opportunity (disposing the source will dispose of this allocated handle). The handle should not be closed or disposed directly.

相反,我发现带有 Task.Delay 的模式更清晰易读:

public static async Task CheckTask(CancellationToken token)
{
do
{
// Do some processing
Console.WriteLine("Processing");

await Task.Delay(1500, token);
} while (!token.IsCancellationRequested);

Console.WriteLine("Bye bye");
}

然后,当您想要停止Task 时,通过CancellationTokenSource 取消它。

关于c# - 使用 ContinueWith 自继续任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28170730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com