-4

I've read at least a dozen articles on it and I think I finally understand. Critique my following explanation.

If I use the async keyword inside a function, it signals to the caller of the function that when it reaches the await keyword inside the function then it can go ahead and do any independent work after the function call until the result of awaited function is returned and then it can go back an finish from where it left off.

user7127000
  • 3,143
  • 6
  • 24
  • 41

2 Answers2

2

The async keyword doesn't affect anything in the callee in terms of await. It's legal to await any Task, regardless of where it came from. Let's look at this example:

async Task Main()
{
    Console.WriteLine("Starting A now.");
    GetResult();
    Console.WriteLine("Finished A now.");

    Console.WriteLine("Starting B now.");
    await GetResult();
    Console.WriteLine("Finished B now.");

    Console.WriteLine("Starting C now.");
    GetResultAync();
    Console.WriteLine("Finished C now.");

    Console.WriteLine("Starting D now.");
    await GetResultAync();
    Console.WriteLine("Finished D now.");
}

Task GetResult()
{
    return Task.Delay(5000).ContinueWith(a => Console.WriteLine("Finished!"));
}

async Task GetResultAync()
{
    await Task.Delay(5000).ContinueWith(a => Console.WriteLine("Finished!"));
}

As you can see - Main is able to await the result regardless of whether the method is async or not. Async simply indicates the compiler that:

  1. The method will be calling async, and needs to be turned into a state machine, and
  2. When writing return a (where a is an int), to wrap the result into Task<int>
Rob
  • 26,989
  • 16
  • 82
  • 98
2

"async" modifier is used on the method declaration/signature to indicate (to the compiler) that the method might invoke one or more asynchronous operations using "await". "async" enables the use of "await" inside the method. The "async" modifier is not used inside a method, it's used on the method signature, like the "public" and "private" keywords.

Something like: (example taken from here:)

public async Task<int> AccessTheWebAsync() { ... }

You can invoke the method above as follows:

Task<int> accessWebTask = AccessTheWebAsync();
... --> code that does not depend on the result of the previous line
var result = await accessWebTask;
... --> code that should not execute until the operation above is done

or shorter form:   
10 var result = await AccessTheWebAsync();
11    ... code after awaited operation

The "await" operator indicates a suspension point, which is to say that the code after the awaited operation requires the awaited operation (line 10) to complete before the rest of the code (line 11 and so on) can be executed. "await" can be used on void returning methods, in this case, there is no waiting, it's a fire and forget operation.

The compiler generates code on your behalf so that all this can work.

Another benefit of "async/await" is in regards to code readability (linear flow, asynchronous code reads like synchronous) and exception handling, most of the time.

This article shows the many ways asynchronous operations can be accomplished (with and without "async/await")

Also keep in mind that "async/await" has very little to do with multithreading. It's meant to help with non blocking operations (db/file system/network) and UI responsiveness. This SO post is a good read. / This series is worth reading also.

And so is this MS article (Synchronous and Asynchronous I/O).

If you are interested in a more conceptual view take a look on this article: The difference between asynchronous and non-blocking (which links to this nice SO post)

And this one is a FAQ on async/await.

Community
  • 1
  • 1
Klinger
  • 4,900
  • 1
  • 30
  • 35
  • "In other words it indicates that the method itself can be awaited on." This is a common misunderstanding, but essential to get right: you do *not* await methods, you only await tasks! There are many situations where you await a task that was not create via an async method. – Voo Nov 26 '16 at 14:10
  • Removed the offending sentence, but just to be clear, the "await" keyword can only be used inside a method marked with the "async" modifier. – Klinger Nov 26 '16 at 19:54