I\'m digging into the async-await
mechanism and observed the throwing of a TaskCanceledException
that I can\'t explain yet.
In the sample b
If you replace the Func
with a method, it passes.
private static async Task TestNullCase()
{
// This does not throw a TaskCanceledException
await Task.Run(() => 5);
// This does throw a TaskCanceledException
await Task.Run(() => GetNull());
}
private static object GetNull()
{
return null;
}
UPDATE
After letting ReSharper convert both lambdas to variables:
private static async Task TestNullCase()
{
// This does not throw a TaskCanceledException
Func func = () => 5;
await Task.Run(func);
// This does throw a TaskCanceledException
Func function = () => null;
await Task.Run(function);
}
So, the second form is incorrectly interpreted as Func
instead of your intent, which I believe is Func
. And because the Task passed in is null, and because you can't execute a null, you get a TaskCanceledException
. If you change the variable type to Func
it works without any additional changes.