3

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:

  1. Thread ‘T’ (e.g. GUI thread) is executing a GUI async function ‘F’
  2. At some point in that async function ‘F’ we call ‘await’ on an “awaitable” object ‘A’ (most probably a Task/Task<>).
  3. 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.
  4. 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.
Rejkid
  • 109
  • 1
  • 6
  • Take a look at Stephen Cleary's blog. For a more academic take on in, Eric Lippert also talks about it on his blog (he starts from the point of view of continuations). – Flydog57 Jan 18 '20 at 05:24
  • Take a look at this: [async/await. Where is continuation of awaitable part of method performed?](https://stackoverflow.com/questions/31186354/async-await-where-is-continuation-of-awaitable-part-of-method-performed). Short answer: it depends on the type of the project, and on whether you have used [`ConfigureAwait(false)`](https://johnthiriet.com/configure-await/). – Theodor Zoulias Jan 18 '20 at 10:43
  • C# also has one UI thread so the same concept applies. Use a [BackgroundWorker](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker) if you need to interact with the UI from a non-UI thread: https://stackoverflow.com/a/1507337/298511 – Mike Lowery Mar 27 '23 at 16:38

3 Answers3

1

first off all the both syntaxes is difference.

Thread Syntax :-

Thread thread = new Thread(() => VoidMethod("","",""));

thread.Start();

https://www.c-sharpcorner.com/blogs/asynchronous-multithreaded-programming-with-example-in-c-sharp

if your using this thread doesn't return any value. while entire request complete.

Task/Task<> Syntax :-

public async Task<int> VoidMethod("","","")
{
   await Task.Run(() => VoidMethod("","",""));
   return 1; 
}

https://www.c-sharpcorner.com/article/async-and-await-in-c-sharp/

if your using this method returns value. while entire request complete.

chandu komati
  • 795
  • 1
  • 5
  • 21
  • if your request taking time. and user consequently, request another request and then until comeple first request user again request. task/await method is not right, – chandu komati Jan 18 '20 at 05:39
  • thread is best way if request taking time and user want to do consequently request access the data . – chandu komati Jan 18 '20 at 05:41
  • I think I understand that, but my question was, In the scenario I have described above what thread is used to execute the awaitable method? – Rejkid Jan 19 '20 at 00:59
0

The best place to start is the docs:

Asynchronous programming with async and await

Than, any blog post by Stephen Toub or Stephen Cleary.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
-1
  1. await() may cause return from the async method, but its execution is not finished, it will be called again some time later. In order to be called later, it gets in the line to some event before return, just like a thread can call to Object.wait() in Java, which means the thread gets in the line to some event and leaves the processor. Async method leaves the thread, as the thread plays the role of processor for it.

  2. when the event occur, the async method is resumed, that is, it is just invoked again on a next available thread of the thread pool - just like a thread after wait() runs on a next available processor. In order to continue execution not from the start, but from the last place where it returned because of await(), the information about the state is saved and then used in the form: switch(state) { case 0: goto 0'; case 1: goto 10'; .... }

Note this is bytecode, so goto can be used. To save the state, we need a field of an object, so async method in fact is not a method but a class, which is instantiated at the place where it is called first time.

Because of direct use of goto, it is hard or sometimes impossible to code async method in plain C# or Java. But it is always possible to code each part of the async method as a separate method and use that methods as callbacks for the awaited events. Async/await is just another syntax for asynchronous programming, along with callbacks, actors, ComplatableFuture, java.util.concurrent.Flow etc.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • When you say "when the event occur, the async method is resumed, that is, it is just invoked again on a next available thread of the thread pool" - is that right?, I thought it must continue on the same GUI thread "T" - not on any pool thread (is C# like Java having only one GUI thread?) – Rejkid Jan 19 '20 at 00:56
  • When an asynchronous method resumes after an asynchronous await, it will attempt to resume in the same "context." If it started on the GUI thread , it will attempt to continue there. If it started outside GUI thread, it will never continue on the GUI thread. See https://social.msdn.microsoft.com/Forums/en-US/269172a3-adb9-4b5e-9ac1-8b67ff920177/async-issuewhy-blocking-the-ui-thread?forum=async – Alexei Kaigorodov Jan 19 '20 at 09:25
  • What do you mean by "it will attempt to continue there" - can it run on a different thread then the GUI one? Isn't GUI thread in C# a single thread like in Java? – Rejkid Jan 22 '20 at 01:43