入力を入力しないと、以下のコードが完了しないのはなぜですか?また、キャンセルトークンがキャンセルされた後でもキーが押されたままになるのはなぜですか?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が.ReadAsyncをオーバーライドしない[__ConsoleStream](https://referencesource.microsoft.com/#mscorlib/system/io/__consolestream.cs,de9f3a925342686c)のインスタンスを返していることに注意してください。 –
次回'var cancelationSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));'のように、適切なコンストラクタを使用してトークンを取り消すための不要な魔法を実行する前に、 https://msdn.microsoft.com/en-us/library/hh139229(v=vs.110).aspx 「 –
@ PeterBons」私のやり方では、私のやり方は、何かが、 –