Tag: async 等待

如何取消Stream.ReadAsync?

如果您没有输入任何输入,为什么下面的代码没有完成?为什么即使在取消令牌被取消后它仍然响应按下的键? // Set up a cancellation token var cancellationSource = new CancellationTokenSource(); // Cancel the cancellation token after a little bit of time Task.Run(async () => { await Task.Delay(TimeSpan.FromSeconds(2)); cancellationSource.Cancel(); Console.WriteLine(“Canceled the cancellation token”); }); // Wait for user input, or the cancellation token Task.Run(async () => { try { using (var input = Console.OpenStandardInput()) […]