Forum Xamarin Xamarin.iOS

async/await issue on Mac


I'm just getting started and found a strange issue with asynchronous code. Try this in an iOS project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Portable;

namespace AsyncAwaitTest
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
        UIWindow window;
        AsyncAwaitTestViewController viewController;

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            Test ().Wait ();

            window = new UIWindow (UIScreen.MainScreen.Bounds);

            viewController = new AsyncAwaitTestViewController ();
            window.RootViewController = viewController;
            window.MakeKeyAndVisible ();

            return true;

        public static async Task Test()
            await Task.FromResult (true);
            await Task.Delay (1000);
            await Task.FromResult (true);

It seems that the await Task.Delay immediately returns without delay and causes control to pass back to the caller - the second call to await Task.FromResult is never even executed! I've tried both via the debugger and by doing Console.WriteLines to see which statement successfully execute. In both cases, it's clear something very wrong is happening when awaiting the delay. No exceptions, no weird application output.

Any ideas?



  • KentBoogaartKentBoogaart AUMember ✭✭

    Thanks Adam. Of course, you're right - that'll teach me for throwing together quick tests without sufficient thought. I was getting confused because the behavior of XS differs from VS. In VS, attempting to step over the await Task.Delay just blocks and its evident in the stack, whereas in XS it appears to continue execution.


  • dash_alirezadash_alireza USMember ✭✭

    I have a problem in this case.

    While LoadViewControllers do his job (for me it's check existing password with server), FinishedLaunching returns true and there will be a Black Screen on mobile ! what can I do?

  • adamkempadamkemp USInsider, Developer Group Leader mod
    My last post describes how to do that for Xamarin.iOS. The concept is the same for Forms. Make your first page be a loading screen page. Replace it when you know which one to use.
  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    @dash_alireza - This thread is from 2014, and by posting on it you've notified everyone on it.

    In addition, your post does not have sufficient information for anyone to help you. It also sounds like you are on iOS (Mobile) and this is the Xamarin.Mac forum.

    Please create a new post in the correct section, with an example or sufficient information.

  • dash_alirezadash_alireza USMember ✭✭
    edited November 2017

    @adamkemp Thanks for your response. I'll check it

    @ChrisHamons Thanks for you notice. :)

  • DooksDooks ZAMember ✭✭✭

    If you just want to delay your Splash screen (Launch Screen) then you can do the following:

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        // Delay the thread
        // Continue loading the next page
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        Window.RootViewController = new UINavigationController(new LoginScreen());
        return true;
  • adamkempadamkemp USInsider, Developer Group Leader mod
    Blocking the UI thread in `FinishedLaunching` is a bad idea. It could lead to your app being killed because it is unresponsive. It’s also just bad for your users. Please don’t do that.
Sign In or Register to comment.