Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awaiting tasks: Return task or await if no code after await [duplicate]

I've done a bit of reading, and think I have grasped the basics of the await and async keywords, with regards to System.Threading.Task.

I'm not sure if I'm right over a small issue, however, and am looking for verification or for someone to correct me.

I'm implementing an async method, with this signature:

public Task ProcessUploadedFile(FileInfo info, string contentType);

Obviously, if I want to await anything inside the method, I need to add the async keyword into the signature,

My question is this: If the last thing that my method does is call another async method, or return a task, is there any point in awaiting it?

Eg.

1:

public async Task ProcessUploadedFile(FileInfo info, string contentType)
{
  foreach (var something in someCollection)
    DoSomething();

  DoSomethingElse();

  await DoMethodAsync();
}

2:

public Task ProcessUploadedFile(FileInfo info, string contentType)
    {
      foreach (var something in someCollection)
        DoSomething();

      DoSomethingElse();

      return DoMethodAsync();
    }

I initially wrote the former, but can no longer see the point in adding the await. If I were to write the latter, I accomplish the same thing, and the caller of both methods can still use the await keyword if they choose, no?

Is there any difference in the above? Which is "better"?

like image 305
dark_perfect Avatar asked Aug 05 '13 15:08

dark_perfect


People also ask

What happens if you do not await a 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. By default, this message is a warning.

Can you await a task twice?

await hides all this complexity from you, and it allows you to await the same task in ten different places (very useful for e.g. asynchronous lazy initialization). can I be assured that the method pointed by task wont be executed twice even if the task is running or ran already ? @BilalFazlani Yes, you can.

Do I have to await task?

If it is some trivial operation that executes quickly, then you can just call it synchronously, without the need for await . But if it is a long-running operation, you may need to find a way to make it asynchronous.

What happens when you call async method without await?

In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously. Code after each await expression can be thought of as existing in a .then callback.


1 Answers

await is used to continue execution of method body when awaiting completed. If there is nothing to continue, then you don't need awaiting.

You can think of await (simplified) as of ContinueWith operation. So, your first method is something like:

foreach (var something in someCollection)
     DoSomething();

DoSomethingElse();

DoMethodAsync().ContinueWith(t => {});

There is nice MSDN article which describes what happens in async method which has nice code flow picture:

enter image description here

like image 104
Sergey Berezovskiy Avatar answered Sep 22 '22 01:09

Sergey Berezovskiy