RequestProductPurchaseAsync throws “Cannot change thread mode after it is set”

1

I'm using monogame as a framework for games and I'm trying to implement a in-app-purchase-functionality in an UWP-App which throws an exception when I'm calling RequestProductPurchaseAsync. It states:

Cannot change thread mode after it is set. (Exception from HRESULT: 0x80010106 (RPC_E_CHANGED_MODE)) at Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(String productId) at Crocs_World__Xbox_Edition_.App.d__7.MoveNext()

That's what I'm doing in code:

public async Task LoadInAppPurchaseProxyFileAsync()   
{
     StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
     StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
     licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
     CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
     await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);

    // setup application upsell message
    try
    {
        ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
        var product1 = listing.ProductListings["product1"];
        var product2 = listing.ProductListings["product2"];               
    }
    catch (Exception e)
    {
        Debug.WriteLine("LoadListingInformationAsync API call failed:" + e);               
    }
}

private async void InAppPurchaseRefreshScenario()
{           
    Debug.WriteLine("InAppPurchaseRefreshScenario");
}

public async Task BuyFeature()
{
    LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
    if (!licenseInformation.ProductLicenses["product2"].IsActive)
    {
        Debug.WriteLine("Buying Product 2...");
        try
        {
            await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
            if (licenseInformation.ProductLicenses["product2"].IsActive)
            {
                Debug.WriteLine("You bought Product 2.");
            }
            else
            {
                Debug.WriteLine("Product 2 was not purchased.");
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine("Unable to buy Product 2." + e);
        }
    }
    else
    {
        Debug.WriteLine("You already own Product 2.");
    }
}

Whenever I call BuyFeature it throws the exception. Except if I call it right in LoadInAppPurchaseProxyFileAsync. Then it seems to be in the same thread I guess. If I replace Task with void in both methods it doesn't work either. It also doesn't matter if I call it from app.xaml.cs or the game.cs. Does anybody have an idea what I'm doing wrong?

Thank you,

Harry

c#
uwp
monogame
asked on Stack Overflow Oct 19, 2017 by harryh100 • edited Oct 19, 2017 by (unknown user)

1 Answer

0

You have to call await CurrentAppSimulator.RequestProductPurchaseAsync(...) in the UI thread, because if you run it in release mode with await CurrentApp.RequestProductPurchaseAsync(...) it shows a "Buy item" content dialog on the screen which is only possible in the UI thread.

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
    await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
};
answered on Stack Overflow Apr 19, 2018 by COM8

User contributions licensed under CC BY-SA 3.0