Task 超时取消

  using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3)))
  {
     // cts.CancelAfter(TimeSpan.FromSeconds(3));
      try
      {
          var token = cts.Token;
         await Task.Delay(5000, token);
      }
      catch (Exception e)
      {
          if (e is TaskCanceledException te)
          {

          }

      }

  }
posted @ 2024-07-09 09:27  [在河之洲]  阅读(3)  评论(0编辑  收藏  举报