gpt4 book ai didi

c# - 使用超时处理多个 CancellationToken

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

我对如何为以下情况实现取消 token 感到有些困惑。

假设我有一个方法,它有一个取消 token ,没有像这样指定超时。

public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
try
{
Task.Delay(1000, cancellationToken)
}
catch (OperationCanceledException canceledException)
{
// Do something with canceledException
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", canceledException);
throw;
}
catch (Exception exception)
{
// Do something with exception
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", exception);
throw;
}
}

但是在那个方法中我想调用另一个需要 CancellationToken 的方法,除了这次我想在它上面设置一个超时,就像这样。

public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
try
{
var innerCancellationTokenSource = new CancellationTokenSource();
innerCancellationTokenSource.CancelAfter(1000);
var innerCancellationToken = innerCancellationTokenSource.Token;

await DoSomeElseAsyncThingAsync(innerCancellationToken);
}
catch (OperationCanceledException canceledException)
{
// Do something with canceledException
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", canceledException);
throw;
}
catch (Exception exception)
{
// Do something with exception
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", exception);
throw;
}
}

如何获取 innerCancellationToken 以遵守来自 cancellationToken 参数的取消请求?

我能想到的最好的是这样的:

public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
try
{
await Task.WhenAny(
DoSomeElseAsyncThingAsync(cancellationToken),
KaboomAsync(100, cancellationToken)
);
}
catch (OperationCanceledException canceledException)
{
// Do something with canceledException
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", canceledException);
throw;
}
catch (Exception exception)
{
// Do something with exception
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", exception);
throw;
}
}

public static async Task KaboomAsync(int delay, CancellationToken cancellationToken = default)
{
await Task.Delay(delay, cancellationToken);
throw new OperationCanceledException();
}

但这并不完全正确; KaboomAsync() 函数总是会崩溃,而且这条路径看起来很粗糙。有更好的模式吗?


发布答案我创建了这个静态 Util 方法,以节省我将样板文件放入一百万次的时间。

希望它对某人有用。

public static async Task<T> CancellableUnitOfWorkHelper<T>(
Func<CancellationToken, Task<T>> unitOfWordFunc,
int timeOut,
CancellationToken cancellationToken = default
)
{
try
{
var innerCancellationTokenSource = new CancellationTokenSource(timeOut);
using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationTokenSource.Token, cancellationToken))
return await unitOfWordFunc(linkedTokenSource.Token);
}
catch (OperationCanceledException canceledException)
{
Console.WriteLine(
cancellationToken.IsCancellationRequested
? "Manual or parent Timeout {0}"
: "UnitOfWork Timeout {0}"
, canceledException
);

throw;
}
catch (Exception exception)
{
Console.WriteLine("Exception {0}", exception);
throw;
}
}

public static async Task CancellableUnitOfWorkHelper(
Func<CancellationToken, Task> unitOfWordFunc,
int timeOut,
CancellationToken cancellationToken = default
)
{
try
{
var innerCancellationTokenSource = new CancellationTokenSource(timeOut);
using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationTokenSource.Token, cancellationToken))
await unitOfWordFunc(linkedTokenSource.Token);
}
catch (OperationCanceledException canceledException)
{
Console.WriteLine(
cancellationToken.IsCancellationRequested
? "Manual or parent Timeout {0}"
: "UnitOfWork Timeout {0}"
, canceledException
);

throw;
}
catch (Exception exception)
{
Console.WriteLine("Exception {0}", exception);
throw;
}
}

它们可以这样使用。

await Util.CancellableUnitOfWorkHelper(
token => Task.Delay(1000, token),
200
);

await Util.CancellableUnitOfWorkHelper(
token => Task.Delay(1000, token),
200,
someExistingToken
);

在这两个示例中,它都会在 200 毫秒后超时,但第二个示例也会考虑手动取消或来自“someExistingToken” token 的超时。

最佳答案

CancellationTokenSource 有一个专门针对这种情况的方法:CreateLinkedTokenSource

在您的示例中,它可能看起来像这样:

public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
try
{
var innerCancellationTokenSource = new CancellationTokenSource();

using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationTokenSource.Token, cancellationToken))
{
innerCancellationTokenSource.CancelAfter(1000);

await DoSomeElseAsyncThingAsync(linkedTokenSource.Token);
}
}
catch (OperationCanceledException canceledException)
{
// Do something with canceledException
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", canceledException);
throw;
}
catch (Exception exception)
{
// Do something with exception
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", exception);
throw;
}
}

请注意,处理链接源很重要,否则来自父标记源的引用将阻止它被垃圾回收。

另见 Any way to differentiate Cancel and TimeoutWhen to dispose CancellationTokenSource?

关于c# - 使用超时处理多个 CancellationToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780925/

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