I am coming from a Java development environment and has started to code in C#. I have noticed the ‘async/await’ pattern in C# that I have never seen in Java. What is it exactly? I have browsed internet for a while and can’t find the definite explanation that would clarify my understanding of what it is.
So let’s define the following scenario:
- Thread ‘T’ (e.g. GUI thread) is executing a GUI async function ‘F’
- At some point in that async function ‘F’ we call ‘await’ on an “awaitable” object ‘A’ (most probably a Task/Task<>).
- Then, the ‘await’ call is going to free/yield (but not suspend) the execution of thread ‘T’ (in this case the GUI thread) in order to run/execute some other Task(s) while ‘awaitable’ ‘A’ is executing its work.
- When the ‘awaitable’ ‘A’ object is finished doing its work the execution of the async function ‘F’ resumes. In the above scenario (if I described it correctly), which thread will execute ‘awaitable’ ‘A’ method? – the GUI thread or some other thread from the pool?. If it is a pool thread (not the GUI thread) and I am accessing in that method GUI resources (e.g. buttons, labels, grid view etc), am I going to corrupt GUI thread data? Remember that I coming from Java world where there is only one GUI thread that can change/manipulate the GUI resources.