InvokeOnUIThread

Hi guys, I’m using InvokeOnUIThread but I need to wait for RequestProductPurchaseAsync to finish before moving onto the if statement. Below is what I have at the moment but it doesn’t seem to work?

Cheers

UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
{
    await CurrentAppSimulator.RequestProductPurchaseAsync(id, false);
}, true);

if (response != null && CurrentApp.LicenseInformation.ProductLicenses[id].IsActive)
{
    response(true);
}

The lambda you pass to InvokeOnUIThread is asynchronous - that means that even if you pass “true” as the second parameter it will only wait until it reaches the first “await” keyword. Do something like this:

void Purchase()
{
    StartCoroutine(DoPurchase());
}

IEnumerator DoPurchase()
{
    bool asyncOperationDone = false;
    UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
    {
        await CurrentAppSimulator.RequestProductPurchaseAsync(id, false);
        asyncOperationDone = true;
    }, false);

    while (!asyncOperationDone)
        yield return null;

    if (response != null && CurrentApp.LicenseInformation.ProductLicenses[id].IsActive)
    {
        response(true);
    }
}

Thanks