Reputation: 4290
This code causes exceptions to get lost.
Instead of boo.DoStuff()
, it should be Task.WaitAll(bar.DoStuff())
.
It's perfectly safe to block because the timer callback is already on a thread pool thread, and I'm not doing anything that would use IOCP. If I don't block, I silently lose any exceptions.
How can I get either VS/ReSharper/something to give me a warning on this line?
public class Foo
{
readonly IBoo _boo;
readonly Timer _timer;
public Foo(IBoo boo)
{
_boo = boo;
_timer = new Timer(Beat, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
void Beat(object state)
{
_boo.DoStuff(); // <-- warning wanted here
// Task.WaitAll(bar.DoStuff()); <-- what I should have written
}
}
public interface IBoo
{
Task DoStuff();
}
Upvotes: 0
Views: 424
Reputation: 171178
I don't know of a tool to provide this warning but Resharper is extensible. They recently open-sourced the code for an extension that marks all allocations in the source code. It seems to be not that hard to write an extension. It would trigger on all method calls that return Task
and do not immediately use the result of the call in any way.
You might face false positives, though, in cases where a method returns a task that is polled for error information elsewhere. Still, this is a useful warning. I often see questions on Stack Overflow that would be solved by not ignoring some task.
Upvotes: 2