Code Inspection: Avoid using 'async' lambda when delegate type returns 'void'
This inspection reports usages of void
delegate types in the asynchronous context. The problem here is the same as with async void methods but it is much harder to spot.
To illustrate the problem, let's consider the following method:
whose doSomething
parameter is of the Action
delegate type, which returns void
. It's safe to use this method in a synchronous context, for example,
but using it in an asynchronous context, for example,
can lead to problems in runtime. Here is an example: suppose we decided to expand the lambda to throw an exception:
Because our doSomething
delegate is void
, the exception will never affect the caller thread and will not be caught with catch
.
If you use RunThisAction()
only with async
arguments, you can fix this problem by using a delegate type that returns Func<Task>
: