Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use await keyword

If a method is declared as 'async', when we call that method, we can use await or not use it. I want to know when to use it and what's the effect of use it.

like image 262
James Avatar asked Nov 28 '12 02:11

James


2 Answers

The async keyword is just an indicator to the compiler that the method may contain an await statement.

There's an interesting post by Eric Lippert explaining the design choice

Requiring "async" means that we can eliminate all backwards compatibility problems at once; any method that contains an await expression must be "new construction" code, not "old work" code, because "old work" code never had an async modifier.

http://blogs.msdn.com/b/ericlippert/archive/2010/11/11/whither-async.aspx

like image 112
Eric J. Avatar answered Oct 01 '22 02:10

Eric J.


The marked async method typically use await to designate suspension points. The await operator tells the compiler that the async method can't continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method.

in another words if you are doing asyncronius programming and want to run certain thread task simultaniusly you would want to rely on async and await

more detailed explanations are here

http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

like image 45
COLD TOLD Avatar answered Oct 01 '22 01:10

COLD TOLD