知道哪个 CancellationToken 在 CreateLinkedTokenSource 中失败以评估奇怪的 cancellationRequested 行为
Know which CancellationToken failed in CreateLinkedTokenSource to evaluate weird cancellationRequested behavior
我有以下代码
Class Class1
{
Class2 class2 = new Class2();
CancellationToken first = new CancellationToken();
CancellationToken second = new CancellationToken();
CancellationTokenSource cts=
CancellationTokenSource.CreateLinkedTokenSource(first, second))
class2.OpenSocketAndRead(cts.Token);
}
Class Class2
{
public void OpenSocketAndRead(CancellationToken ct)
{
try
{
await websocket.ReceiveAsync(new ArraySegment<byte>(buffer, receivedBytes, availableBytes), ct).ConfigureAwait(false);
}
catch (WebSocketException e)
{
ct.ThrowIfCancellationRequested();
var message = this.ConstructErrorMessage("Error during read.", ct);
throw new CustomException(message);
}
}
private string ConstructErrorMessage(String m, CancellationToken ct)
{
StringBuffer sb = new StringBuffer();
sb.append(m);
if(ct.IsCancellationRequested)
{
sb.append("WHICH TOKEN FAILED");
}
}
}
我遇到了这种奇怪的行为,其中 ct 没有抛出并且流来到 ConstructErrorMessage,其中 ct.IsCancellationRequested 评估为真。 Websocket 错误代码是 ConnectionClosedPrematurely。
我想知道哪个令牌在 ConstructErrorMessage 中失败以及为什么 ThrowIfCancellationRequested 没有执行。
I experienced this weird behavior where ct didnotthrow and the flow came to ConstructErrorMessage where ct.IsCancellationRequested evaluates to true.
这完全有可能 - 这意味着 CancellationToken
在调用 ThrowIfCancellationRequested
之后和调用 IsCancellationRequested
.
之前被取消了
I want to know which token failed
这在一般情况下是不可能的,因为链接令牌。但是,您的代码可以检查哪些标记被取消,并且出于所有实际考虑,这通常就足够了。
我有以下代码
Class Class1
{
Class2 class2 = new Class2();
CancellationToken first = new CancellationToken();
CancellationToken second = new CancellationToken();
CancellationTokenSource cts=
CancellationTokenSource.CreateLinkedTokenSource(first, second))
class2.OpenSocketAndRead(cts.Token);
}
Class Class2
{
public void OpenSocketAndRead(CancellationToken ct)
{
try
{
await websocket.ReceiveAsync(new ArraySegment<byte>(buffer, receivedBytes, availableBytes), ct).ConfigureAwait(false);
}
catch (WebSocketException e)
{
ct.ThrowIfCancellationRequested();
var message = this.ConstructErrorMessage("Error during read.", ct);
throw new CustomException(message);
}
}
private string ConstructErrorMessage(String m, CancellationToken ct)
{
StringBuffer sb = new StringBuffer();
sb.append(m);
if(ct.IsCancellationRequested)
{
sb.append("WHICH TOKEN FAILED");
}
}
}
我遇到了这种奇怪的行为,其中 ct 没有抛出并且流来到 ConstructErrorMessage,其中 ct.IsCancellationRequested 评估为真。 Websocket 错误代码是 ConnectionClosedPrematurely。
我想知道哪个令牌在 ConstructErrorMessage 中失败以及为什么 ThrowIfCancellationRequested 没有执行。
I experienced this weird behavior where ct didnotthrow and the flow came to ConstructErrorMessage where ct.IsCancellationRequested evaluates to true.
这完全有可能 - 这意味着 CancellationToken
在调用 ThrowIfCancellationRequested
之后和调用 IsCancellationRequested
.
I want to know which token failed
这在一般情况下是不可能的,因为链接令牌。但是,您的代码可以检查哪些标记被取消,并且出于所有实际考虑,这通常就足够了。