3

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?

1
  • 2
    By “Android”, do you mean “Java on Android”? Have you considered Xamarin.Android, which allows you to use C#, including await, to write Android apps?
    – svick
    Aug 6, 2014 at 15:32

1 Answer 1

2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.