# InvokeOnUIThread

**URL:** https://discussions.unity.com/t/invokeonuithread/613971
**Category:** Desktop
**Tags:** Windows
**Created:** [January 24, 2016, 10:45pm UTC](https://discussions.unity.com/t/invokeonuithread/613971 "2016-01-24T22:45:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jackal16](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@Jackal16](https://discussions.unity.com/u/Jackal16)
#### Post date: [January 24, 2016, 10:45pm UTC](https://discussions.unity.com/t/invokeonuithread/613971/1 "2016-01-24T22:45:33Z")

</div>

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

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

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

```

---

<div class="post-metadata">

### Author: ![Tautvydas-Zilys](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tautvydas-Zilys](https://discussions.unity.com/u/Tautvydas-Zilys)
#### Post date: [January 25, 2016, 8:50am UTC](https://discussions.unity.com/t/invokeonuithread/613971/2 "2016-01-25T08:50:45Z")

</div>

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:

```auto
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);
    }
}

```

---

<div class="post-metadata">

### Author: ![Jackal16](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@Jackal16](https://discussions.unity.com/u/Jackal16)
#### Post date: [January 25, 2016, 8:24pm UTC](https://discussions.unity.com/t/invokeonuithread/613971/3 "2016-01-25T20:24:05Z")

</div>

Thanks
