Forum Xamarin.Forms

Xamarin.Forms Async Confusion

I'm a bit confused about async. If I run animations like content.ScaleTo(1, 300, Easing.CubicInOut) awaited in an async function will that be ok or does it cause an issue since it's not on the main thread?

Posts

  • Jesse_JiangJesse_Jiang USMember ✭✭✭

    await will block the function, like
    A();
    await B();
    C();
    Only B is finished, C can be called.

    if you do
    A();
    B(); //async function
    C();
    After A, it will call C() directly, no wait B() finished.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @JoshuaNovak.6915 - Now that we have the source code, this question is easy to answer.

    Animations or anything to do with the UI must be done on the main thread, you are correct there.

    However in the ScaleTo (or other animation code) you can go all the way down and see this piece of code

    if (Device.IsInvokeRequired)
    {
        Device.BeginInvokeOnMainThread(animate);
    }
    else
    {
        animate();
    }
    

    Meaning it will place it on the UI thread when it needs to but everything else can be running on a different thread.

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @AdamP Thanks! That's exactly what I was looking for. I saw many articles saying you needed to put it on the main thread and many saying async works. I guess they were both right lol.

Sign In or Register to comment.