Last active
October 15, 2023 04:39
-
-
Save in-async/25d7f2195c0c07ddf445fcff3269f0e6 to your computer and use it in GitHub Desktop.
どっちも同じように OperationCanceledException を投げてるだけなのに、 Task.Status が異なるのなーんでだ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CancellationToken ct = new CancellationTokenSource(0).Token; | |
Task task1 = Task.Run(() => throw new OperationCanceledException(ct)); | |
Task task2 = Task.Run(() => ct.ThrowIfCancellationRequested()); | |
Task.WhenAll(task1, task2).ContinueWith(_ => { | |
task1.Status.Dump(); // Canceled | |
task2.Status.Dump(); // Faulted | |
}); |
そもそも
Task.Run(() => throw new OperationCanceledException(ct));
が Task.Run(Func<Task>)
にオーバーロード解決されてるのが直感的におかしいんだけどな。仕様らしい。
ref.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
教訓
Task.Run(...)
を使う際は、どのオーバーロードを使用しているか意識する事。Task.Run(...)
には似たようなオーバーロードが多く、呼び間違えやすいので、注意する必要がある。