Under what scenarios would one want to use
public async Task AsyncMethod(int num)
instead of
public async void AsyncMethod(int num)
The only scenario that I can think of is if you need the task to be able to track its progress.
Additionally, in the following method, are the async and await keywords unnecessary?
public static async void AsyncMethod2(int num)
{
await Task.Factory.StartNew(() => Thread.Sleep(num));
}
I got clear idea from this statements.
Exceptions from an Async Void Method Can’t Be Caught with Catch
These exceptions can be observed using AppDomain.UnhandledException or a similar catch-all event for GUI/ASP.NET applications, but using those events for regular exception handling is a recipe for unmaintainability(it crashes the application).
Async void methods have different composing semantics. Async methods returning Task or Task can be easily composed using await, Task.WhenAny, Task.WhenAll and so on. Async methods returning void don’t provide an easy way to notify the calling code that they’ve completed. It’s easy to start several async void methods, but it’s not easy to determine when they’ve finished. Async void methods will notify their SynchronizationContext when they start and finish, but a custom SynchronizationContext is a complex solution for regular application code.
Async Void method useful when using synchronous event handler because they raise their exceptions directly on the SynchronizationContext, which is similar to how synchronous event handlers behave
For more details check this link https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
The problem with calling async void is that you don’t even get the task back, you have no way of knowing when the function’s task has completed (see https://blogs.msdn.microsoft.com/oldnewthing/20170720-00/?p=96655)
Here are the three ways to call an async function:
My answer is simple you cannon await void method Error CS4008 Cannot await 'void' TestAsync e:\test\TestAsync\TestAsync\Program.cs
So if the method is async it is better to be awaitable, because you can loose async advantage.
I have come across this very usefull article about
async
andvoid
written by Jérôme Laban: http://www.jaylee.org/post/2012/07/08/c-sharp-async-tips-and-tricks-part-2-async-void.aspxThe bottom line is that an
async+void
can crash the system and usually should be used only on the UI side event handlers.I think you can use
async void
for kicking off background operations as well, so long as you're careful to catch exceptions. Thoughts?1) Normally, you would want to return a
Task
. The main exception should be when you need to have avoid
return type (for events). If there's no reason to disallow having the callerawait
your task, why disallow it?2)
async
methods that returnvoid
are special in another aspect: they represent top-level async operations, and have additional rules that come into play when your task returns an exception. The easiest way is to show the difference is with an example:f
's exception is always "observed". An exception that leaves a top-level asynchronous method is simply treated like any other unhandled exception.g
's exception is never observed. When the garbage collector comes to clean up the task, it sees that the task resulted in an exception, and nobody handled the exception. When that happens, theTaskScheduler.UnobservedTaskException
handler runs. You should never let this happen. To use your example,Yes, use
async
andawait
here, they make sure your method still works correctly if an exception is thrown.for more information see: http://msdn.microsoft.com/en-us/magazine/jj991977.aspx