Android – How to Implement Await in Android

androidcsynchronization

This is a general question to understand how await in C# works, and particularly I am interested in implementing it in Android.

So C# await allows you to go in to waiting mode in the middle of the method until the the task you are waiting for is complete, then you will continue the method execution.
How ever the interesting part is while you are waiting inside the method, the thread that you are working on is not getting locked(or at list it does not looks like it got locked) and the code will continue running from where you called that method.

I want to understand what is happening there in C# and how can I implement it in Android?

Best Answer

How it works is fully explained here. Unfortunately, it's mostly implemented in the compiler. You might be able to do it by implementing a state machine and some lambda expressions, but by the time you do all that, callbacks are almost going to be simpler.

There is a non-compiler implementation/replacement for Async/Await in this Code Project article that you might be able to translate into Java. It works by spinning off background threads in a process he calls "cooperative threading," rather than reordering method calls as the C# compiler does.

References
Code Project: Async/Await could be better
Stack Overflow: How to Await without Async CTP
MSDN: Asynchronous Programming with Async and Await (C# and Visual Basic)

Related Topic