Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

c# - Task.Run().Wait() is not working in Onstart() but it is working in App constructor of Xamarin.Forms

I am facing with an interesting issue which I couldnt figure out what might be the cause.

I am trying to handle an asynchronous task to check if there is active subscription for my app. It is working if it is done in App Constructor. But it doesnt work when it is placed in Onstart()

Here; It is working as I expect. It calls BillingTasks.WasItemPurchasedAsync() and wait until it is completed.

    public App()
    {
        ....
        
        if (!Properties.ContainsKey("IsSubscribed"))
        {
            bool WasPurchased = false;

            Task.Run(async () =>
            {
                WasPurchased = await BillingTasks.WasItemPurchasedAsync();
            }).Wait();
            Application.Current.Properties["IsSubscribed"] = WasPurchased;
        }
        ....
    }

But when I put the same code in OnStart(), It seems the task never ends and it crash. So what might be the reason for such different behaviour?

    protected override void OnStart()
    {   
        ....
        
        if (!Properties.ContainsKey("IsSubscribed"))
        {
            bool WasPurchased = false;

            Task.Run(async () =>
            {
                WasPurchased = await BillingTasks.WasItemPurchasedAsync();
            }).Wait();
            Application.Current.Properties["IsSubscribed"] = WasPurchased;
        }
        ....
    }
question from:https://stackoverflow.com/questions/65931071/task-run-wait-is-not-working-in-onstart-but-it-is-working-in-app-construct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From Async/Await - Best Practices in Asynchronous Programming document, suggest prefer async Task methods over async void methods.

OnStart() is not an event handler, override this method to perform actions when the application starts.

As a work around you can create your own custom event and handler that will allow for an async void to be performed on your event handler. You will subscribe to the event when OnStart is invoked by the application and then raise the custom event to be handled asynchronously.

 private event EventHandler Starting = delegate { };

    protected override void OnStart()
    {
        //subscribe to event
        Starting += onStarting;
        //raise event
        Starting(this, EventArgs.Empty);
    }

    private async void onStarting(object sender, EventArgs args)
    {
        //unsubscribe from event
        Starting -= onStarting;
        //perform non-blocking actions
        await checksubscriptionAsync();
    }

    private async Task checksubscriptionAsync()
    {
        await ...
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...