Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to safely wait in Xamarin.Android?

I feel like I'm missing something, as I've seen many, many suggestions of using Task.Delay(ms) for waiting in a mobile application. However, this is dangerous, at least it certainly is on Android, as there is no way to pause that wait should the OS interrupt your Activity to take a phone call or something. If that happens, the delay keeps happily ticking along, returning into a method that is now paused with a view that's been disposed. Sure, I could check for if the state has been saved or something after every single delay, but that feels like a hack and makes me think I must be doing something wrong.

tl:dr I'm making a game, I need to wait on occasion to allow animations and such to play out and let the player sit for a second or two, and I need to be able to pause this delay if OnPause() is called, resume the delay when OnResume() is called, and pick up where I left off. Is there no safe way to do this seemingly simple task using Xamarin.Android? Task.Delay(ms) works astoundingly well unless the OS interrupts my app and then everything falls apart.``

Best Answer


  • BarkingPigBarkingPig Member ✭✭

    Thanks, @YelinZh. That's what I was looking for. I knew it was possible, as there are plenty of Android apps that don't crash and still manage to pause and whatnot, I just needed someone to point me in the right direction. I built a quick prototype and tested out the functionality and it worked just as I needed. Thanks again.

  • BarkingPigBarkingPig Member ✭✭
    edited August 16

    Alright, I threw together a prototype method on my GameActivities that looks something like this:

    public async Task Wait(int msToWait)
        int totalPausedMs = 0;
        PauseTokenSource pts = new PauseTokenSource();
        while (totalPausedMs < msToWait)
            totalPausedMs += 10;
            await Task.Delay(10);
            await pts.Token.WaitWhilePausedAsync();

    For the most part, it works as intended. However, I'm noticing a significant difference between the time the app is paused when compared to Task.Delay(ms). For example, Task.Delay(2000) vs Wait(2000) sees almost an entire second difference that I cannot explain. It's almost as if my Wait(2000) waits for ~3000 total milliseconds and I don't understand why. Any thoughts? Did I implement something in a screwy way?

    It feels like if I edit my step for the while loop to something like 100, 250, or even 500 ms from 10 ms, it seems more responsive, but I'm grasping to understand why. I certainly expect a small ms loss for checking that many times, but I wouldn't expect it to be so noticeable.

Sign In or Register to comment.