Desktop-bridge and Windows APP SDK - This function must be called from a UI thread

199 views Asked by At

I am trying to implement in-app purchase into my WinRT appliation using desktop bridge and Windows App SDK with WinUI 3. I have simple method:

class TransactionService
{
   void buyOrRestore() {

       ....

       ComPtr<IAsyncOperation<StorePurchaseResult*>> purchaseOperation;
       hr = storeContext->RequestPurchaseWithPurchasePropertiesAsync(HStringReference(kItemStoreId).Get(), purchaseProperties.Get(), &purchaseOperation);
       CheckHr(hr);
   }
}

Following code always print error into output:

info:StoreContextServer::Initialize: packageFullName = PurchaseTester, productStoreId = 123, isLicensed = true, isAppContainer = false  [Windows::Services::Store::Internal::StoreContextServer::Initialize]
 info:Windows::Services::Store::StoreContext::RequestPurchaseWithPurchasePropertiesAsync(abc) invoked. (CV:8glMygpFo0+UMRKk.2.3)    [Windows::Services::Store::StoreContext::RequestPurchaseWithPurchasePropertiesAsync]
Exception thrown at 0x00007FFE7BB5474C (KernelBase.dll) in PurchaseTester.exe: WinRT originate error - 0x80070578 : 'This function must be called from a UI thread'.

buyOrRestore method is called directly from MainWindow.xaml.cpp after button click like:

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    auto transactionService = new TransactionService();
    transactionService->buyOrRestore();
}
1

There are 1 answers

0
Mart On

I don't know if this helps you but I had a similar issue of 'This function must be called from a UI thread'. I had my own (desktop) thread which needed to cause a UI event when a packet was received. The following code called from (desktop) thread gets the UI thread to instigate the call to raise the event from the UI thread.

  try
   {


      DispatcherQueue().TryEnqueue([this, hstringItem]()
                                   {
                                      eventPropertyChangedEventHandler(*this, PropertyChangedEventArgs(hstringItem));
                                   });
   }


   catch (...)
   {
   }