user6048670
user6048670

Reputation: 2887

From the point of view of the caller, how is calling an asynchronous method any different than calling a synchronous method?

What I don't understand is the following snippet from MSDN:

Note that the method is now marked with the new async keyword; this is simply an indicator to the compiler that lets it know that in the context of this method, the keyword await is to be treated as a point where the workflow returns control to its caller and picks up again when the associated task is finished.

How is that any different than how non-async methods work?

If I do

int x; 
x = SomeNormalFunctionThatReturnsAnInt(); 
Console.WriteLine(x);

or

int x; 
Task<int> task = SomeAsyncFunctionThatReturnsAnInt();
x = await task; 
Console.WriteLine(x);

then from the perspective of the caller, the order of execution is the exact same: an int named x is defined, a function that returns an int is run, and when that funcion is done running, its return value is set to x, which is then written to the console.

Upvotes: 0

Views: 78

Answers (3)

Eidivandi
Eidivandi

Reputation: 1

you need to have some read on the async and sync to see what happens in performance level ,

in your example the result is not different as you are saying the method to wait in line of await but lets have a look at the following code snippet

class Program {
  private static string result;

  static void Main() {
    SaySomething();
    Console.WriteLine(result);
  }

  static async Task<string> SaySomething() {
    await Task.Delay(5);
    result = "Hello world!";
    return “Something”;
  }
}

can you try to calculate the output, the result is nothing, why ?

with the await we let the main task continue as we wait 5 milliseconds before returning result, have a look at

https://msdn.microsoft.com/en-gb/library/mt674882.aspx

Upvotes: 0

Stephen Cleary
Stephen Cleary

Reputation: 456507

from the perspective of the caller, the order of execution is the exact same

Yes and no.

If you await all tasks as soon as they are returned to you, then yes, that method in isolation is seeing the same "order of execution". This is actually the entire point of async/await - it allows writing most asynchronous code in a way that is very natural and similar to equivalent synchronous code.

However, the caller must be aware that it has to be asynchronous. That is, the caller generally uses await, which means that it must be async. There are some added twists that come in with asynchronous code. One example: if this is executed on a UI thread, then synchronous code knows that nothing else can execute on the UI thread between SomeNormalFunctionThatReturnsAnInt and Console.WriteLine; however, asynchronous code uses await, so it must accept that anything else can execute on the UI thread between SomeAsyncFunctionThatReturnsAnInt and Console.WriteLine. So, looking at it from that context, it's not the exact same; asynchronous methods may have their "order of execution" paused while other code runs.

Upvotes: 2

Ckram
Ckram

Reputation: 586

If you weren't waiting the result of the function x could be not initialized. But here you wait for it so there is no difference.

Upvotes: 0

Related Questions