I\'m trying to familiarize myself with c#\'s new await/async keywords, and I\'ve found several aspects which I can\'t quite understand.
Let\'s start with ra
You can find answer for your questions about await
operator in the following post by Eric Lippert, where he says that:
The “await” operator … does not mean “this method now blocks the current thread until the asynchronous operation returns”. That would be making the asynchronous operation back into a synchronous operation, which is precisely what we are attempting to avoid. Rather, it means the opposite of that; it means “if the task we are awaiting has not yet completed then sign up the rest of this method as the continuation of that task, and then return to your caller immediately; the task will invoke the continuation when it completes. -- Eric Lippert
I recommend you read my async intro.
will this work as expected all the time (e.g. write to the file 12345..... and not 13254 or something)?
No. You need to await
the calls to WriteAsync
.
How much of an async function is executed before it releases to the caller function?
Until it await
s an operation that is not already completed.
Will Thread.Sleep block the whole thread in which it is executed or just the async function?
Thread.Sleep
- and all other blocking methods - will block the async
method and the thread that is executing it.
As a general rule, do not use any blocking methods within an async
method.
What if I use semaphore.Wait() in one of the async functions and it will expect for the semaphore to be released by an other async function. Will this behave as it would with threads, or will it cause deadlock?
It totally depends on your contexts. Wait
is a blocking method, so if the "other" async
method requires the context held by the blocked method, then you will deadlock.
Note that SemaphoreSlim
is async
-friendly; you can use WaitAsync
instead of Wait
.
await does not work outside of async functions. Why?
Because the async
keyword enables the await
keyword. This was done to minimize the impact of new keywords on the C# language and for code readability.
In short, it seems like the answer is "very". While the following may not answer all your questions, I think it applies to a general enough use case. Think of your Stream instead of the network IO they're referring to.
In the case of overlapped network IO, an IO completion port will be used, the callback being triggered by a hardware interupt.
This means that while we "await" completion, a thread will not be consumed. [...]
It is possible to do everything on 1 thread. [...] It will however depend on the platform, the "awaiter" implementation & the synchronization context being used.
http://social.msdn.microsoft.com/Forums/en-US/async/thread/a46c8a54-f8d4-4fa9-8746-64d6a3a7540d/