Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"async Task then await Task" vs "Task then return task" [duplicate]

Quick question..

In order to get some solid base understanding about Asynchronous Programming and the await I would like to know what the difference is between these two code snippets when it comes to multi threading and the execution sequence and time:

This:

public Task CloseApp() {         return Task.Run(                          ()=>{                                  // save database                                 // turn off some lights                                 // shutdown application                           }); } 

Versus this:

public async Task CloseApp() {         await Task.Run(                          ()=>{                                  // save database                                 // turn off some lights                                 // shutdown application                           }); } 

if I am calling it in this routine:

private async void closeButtonTask() {     // Some Task 1     // ..      await CloseApp();      // Some Task 2     // .. } 
like image 797
Khalil Khalaf Avatar asked Jun 24 '16 15:06

Khalil Khalaf


People also ask

Should I return task or await?

Simply return the Task is enough. "when you hit the await, the control flow is returned to the calling method" -- The control flow might be returned to the calling method, particularly it won't return when the awaited task is already complete.

What happens when a method returns a task without awaiting it?

An exception that's raised in a method that returns a Task or Task<TResult> is stored in the returned task. If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown. As a best practice, you should always await the call.

What is the difference between task and async?

Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

What does an async task return?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

When should I return a task from async methods?

In most cases, when an async method calls another and there’s no chain (e.g. public method calling a private method directly) return Task is fine, and avoids the overhead of the await state machine for that method.

What is the difference between await and task in JavaScript?

Use async Task for methods that don't return any results. await awaits an already active asynchronous operation to complete without blocking the caller. You can't compare await with a method returning Task because they simply don't do the same things.

What is the difference between await and async/await?

As there is no more code after your await, there is no need to use await anyway. Simply return the Task is enough. There are very few differences between the two approaches. Basically, they share the same semantics. However, the version with async/await wraps the execution of the inner task in an outer compiler-generated task.

Is it better to use async task or async task< Bool>?

It is always advisable to use async Task or async Task<bool> or async Task<int> just await the method while calling to check every happened well or not. Do you mean exception handling is different in the two cases? You cannot handle exception if thrown from async void methods.


1 Answers

It is almost the same (in terms of threads etc.). But for the second one (using await) a lot more overhead will be created by the compiler.

Methods declared as async and using await are converted into a state machine by the compiler. So when you hit the await, the control flow is returned to the calling method and execution of your async method is resumed after the await when the awaited Task has finished.

As there is no more code after your await, there is no need to use await anyway. Simply return the Task is enough.

like image 99
René Vogt Avatar answered Nov 04 '22 18:11

René Vogt