I burst into the mechanism async-awaitand watched the casting TaskCanceledException, which I still can not explain.
In the example below (self-sufficient) I have a statement
await Task.Run(() => null);
I know that this statement in itself is useless, but I isolated the problem, the real code has logic and in some cases returns null.
Why is it throwing TaskCanceledException? If I return an arbitrary number (5 in the example below), it does not throw.
Also, if I am a awaitmethod, the VS debugger is broken, but if I am not await, then only the message is written to the VS output window.
internal class Program
{
private static void Main(string[] args)
{
var testAsync = new TestAsync();
testAsync.TestAsyncExceptionOnlyInTheOutputWindow();
testAsync.TestAsyncExceptionBreaksIntoTheDebugger();
Console.ReadKey();
}
}
internal class TestAsync
{
public async void TestAsyncExceptionOnlyInTheOutputWindow()
{
TestNullCase();
}
public async void TestAsyncExceptionBreaksIntoTheDebugger()
{
await TestNullCase();
}
private static async Task TestNullCase()
{
await Task.Run(() => 5);
await Task.Run(() => null);
}
}