如果您不輸入任何輸入,爲什麼下面的代碼不會完成?爲什麼即使在取消令牌被取消後,它仍然響應按下的鍵?如何取消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())
{
var buffer = new byte[1];
Console.WriteLine("Waiting for input");
await input.ReadAsync(buffer, 0, 1, cancellationSource.Token); // This is impossible to cancel???
Console.WriteLine("Done waiting for input"); // This never happens until you press a key, regardless of the cancellation token
}
}
catch (Exception e)
{
Console.WriteLine(e.Message); // No errors
}
})
.Wait(); // Block until complete
The documentation for Stream.ReadAsync says:
如果在完成前取消操作,返回的任務包含狀態屬性的取消值。
這意味着取消取消令牌將取消操作,對吧?然而,出於某種原因the source code for Stream.ReadAsync不會做與取消標記任何東西,如果它沒有事先取消:
public virtual Task<int> ReadAsync(Byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
// If cancellation was requested, bail early with an already completed task.
// Otherwise, return a task that represents the Begin/End methods.
return cancellationToken.IsCancellationRequested
? Task.FromCancellation<int>(cancellationToken)
: BeginEndReadAsync(buffer, offset, count);
}
因此取消標記參數是沒有意義的 - 我怎樣才能取消異步讀?
請注意,Console.OpenStandardInput正在返回[__ConsoleStream](https://referencesource.microsoft.com/#mscorlib/system/io/__consolestream.cs,de9f3a925342686c)的一個實例,該實例不會覆蓋.ReadAsync –
下次在一段時間後使用適當的構造函數做一些不必要的魔術來取消令牌:'var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));'。 https://msdn.microsoft.com/en-us/library/hh139229(v=vs.110).aspx ' –
@PeterBons對我來說,我的方式更可讀地傳達一些事情將被打印輸出到控制檯後 –