async/await issue on Mac

Hi,

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?

Thanks

Posts

  • 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.

    Cheers

  • 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 USXamarin 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 10

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

    @ChrisHamons Thanks for you notice. :)

Sign In or Register to comment.