Edit - After reading the three answers below, I've realized that I'm asking nonsense ! Apologies for wasting some of your precious time, and thanks for clarifying alot of misunderstandings, really! :)
I were assuming apples and pipe wrenches were the same thing in the way async/await and threads are solution for similar concerns. Async/await
and Threads
both addresses UI responsiveness, but not in the same way. The former is a pattern to be used when several desired results can be gathered asynchronously (in order to improve user experience for example), hence why it's called "async". Heavy works, those that take several seconds to hours has to be off loaded on another thread to keep UI responsive. Those two are completely different concerns.
No matter how I may edit this question, there is no way to ask such async/await with or without threading concern as they are completely different things, can be used together (in so many ways) or separately. The statement .2 below said it all (apart a "invocation" misconception) but I didn't truely understood its true meaning from the start.
I guess I should have deleted the question from the moment I had doubts, but I couldn't understand how far it was a dumb one before actually reading some answers. However, this may also help other to have a better understanding of what async/await is about.
By the way, I don't know what answer to mark as answer as they all have valuable informations.
Original post:
It's fair to note that I'm "yet another async/await beginner". However, What I've understood so far are:
- async/await doesn't create
threads
! Never ! They run on the same thread as the caller. - async/await are not parallel tasks. They just defines a pattern where an async block of code get executed the moment an await is invoked at caller level (or so - I know my understanding is slightly wrong, but that's how I can clearly picture it in mind)
- No code block is executed asynchronuously if that block doesn't contain an await keyword.
- async/await appears to work well with some I/O tasks, but seems not that wonderful on most high CPU computations.
- async/await seems to fail with
Thread.Sleep
(should useTask.Delay
instead) or in case ofException
withFinally
block.
Based on the above, and to simplify things, I think async/await is just an improved form of goto
, capable of jumping from a method step to another method step, because the goto
call can "fly" (be delayed) until an await is encountered ! (sorry to simplify things that far)
I've tested both I/O and CPU codes without additional threads, both heavy, and both produced unresponsive UI until everything was completed.
My observaton is:
Without asynchronous capable things like :
- threads explicitely defined using one of the numerous
.net
objects in the framework, or their members that create threads on their own - or
IO devices
that are designed to work asynchronously from the start at hardware level (CPU)
async/await
are just plain synchronous patterns; meaning, there is no asynchronous-like thing anywhere with just async/await
keywords.
The question is: is the above observation true, or am I missing some important thing about
async/await
?
Side notes :
I've read this blog post (There Is No Thread, Stephen Cleary's Blog). He states that there is no thread (totally agree) but it doesn't contradict with my observation that, at pure programmer code level without asynchronous devices involved, you can't have a responsive UI unless you delegate the heavy tasks in another thread. My concern is not the awaitable aspect of the pattern, as I said, it's like a flying goto; I want to make sure I'm not using async/await
blindly just for the sake of using something new.
.ConfigureAwait(false)
: seems to involve a ThreadPool to me, which is why the UI thread is still responsive...