Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 了解CreateLinkedTokenSource中哪个CancellationToken未能评估古怪的cancellationRequested行为_C#_Cancellation_Cancellationtokensource_Cancellation Token - Fatal编程技术网

C# 了解CreateLinkedTokenSource中哪个CancellationToken未能评估古怪的cancellationRequested行为

C# 了解CreateLinkedTokenSource中哪个CancellationToken未能评估古怪的cancellationRequested行为,c#,cancellation,cancellationtokensource,cancellation-token,C#,Cancellation,Cancellationtokensource,Cancellation Token,我有以下代码 Class Class1 { Class2 class2 = new Class2(); CancellationToken first = new CancellationToken(); CancellationToken second = new CancellationToken(); CancellationTokenSource cts= CancellationTokenSource.CreateLinked

我有以下代码

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");
       }
   } 
}    
Class1类
{
Class2 Class2=新的Class2();
CancellationToken first=新的CancellationToken();
CancellationToken second=新的CancellationToken();
取消令牌源cts=
CancellationTokenSource.CreateLinkedTokenSource(第一个,第二个))
class2.OpenSocketAndRead(cts.Token);
}
二级
{
公开作废OpenSocketAndRead(取消令牌ct)
{
尝试
{
wait websocket.ReceiveAsync(新的ArraySegment(buffer,receivedBytes,availableBytes),ct);
}
捕获(WebSocketException e)
{
ct.ThrowIfCancellationRequested();
var message=this.ConstructErrorMessage(“读取期间出错”,ct);
抛出新的CustomException(消息);
}
}
私有字符串ConstructErrorMessage(字符串m,CancellationToken ct)
{
StringBuffer sb=新的StringBuffer();
sb.追加(m);
如果(ct.IsCancellationRequested)
{
sb.append(“哪个令牌失败”);
}
} 
}    
我经历了这样一种奇怪的行为,ct没有抛出,流到达constructionErrorMessage,其中ct.IsCancellationRequested的计算结果为true。Websocket错误代码为ConnectionClosedPremary

我想知道ConstructErrorMessage中哪个令牌失败,以及为什么ThrowIfCancellationRequested没有执行

我经历了这样一种奇怪的行为,ct没有抛出,流到达constructionErrorMessage,其中ct.IsCancellationRequested的计算结果为true

这是完全可能的-这意味着
CancellationToken
在调用
ThrowIfCancellationRequested
之后和调用
IsCancellationRequested
之前被取消

我想知道哪个令牌失败了

这在一般情况下是不可能的,因为链接的令牌。然而,您的代码可以检查哪些令牌被取消,对于所有实际考虑,这通常已经足够好了