2

I am trying to have a callback function in xamarin forms to alert the user that the task is finished. I haven't figured it out yet and am not sure what is wrong. Any help is appreciated with the proper way of doing this.

The code below is in a button click event. The method FinishPurchase never gets called.

Task<bool> task = StockDataController.Instance.WasItemPurchased(CloudInfo.IAPMonthlySubscriptionProductID);
Task continuation = task.ContinueWith(t=>FinishPurchase());
continuation.Wait();

Previously I was calling the code below in the button click event but I want to execute specific code after the method WasItemPurchased is executed,etc.

bool result=await StockDataController.Instance.WasItemPurchased(CloudInfo.IAPMonthlySubscriptionProductID)
Jason Smith
  • 373
  • 2
  • 6
  • 20
  • 3
    First code is expected to deadlock as you've likely already investigated (https://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock) so ignoring that... The rest is very unclear - what problem you have to call `FinishPurchase` after `await WasItemPurchased(...)`? – Alexei Levenkov Apr 06 '18 at 23:16
  • thank you. I thought FinishPurchase was being called before await WasItemPurchased was finished executing. I was wrong. Calling await WasItemPurchased (); FinishPurchase() works. – Jason Smith Apr 07 '18 at 01:38

1 Answers1

0

The if statement, below, will be called once WasItemPurchased completes.

bool isResultSuccessful = await StockDataController.Instance.WasItemPurchased(CloudInfo.IAPMonthlySubscriptionProductID);

if (isResultSuccessful)
{
    //Do Something
}
else
{
    //Do Something else
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123