6

Exception generated on MessageBox. How can I use MessageBox in async method?

private async void Purchheard(object sender,EventArgs e)
{
    Debug.WriteLine("Начинаю покупку");
    try
    {
        await CurrentApp.RequestProductPurchaseAsync(ID,false);
        if(license.ProductLicenses[ID].IsActive)
        {
            world.is_freemium=false;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Finished!");
    }
}
Gabriel
  • 377
  • 1
  • 3
  • 15
Vladislav
  • 297
  • 3
  • 13

2 Answers2

5

Not sure why the accepted answer doesn't work, but here is a working example for .NET 4.5

var dg = new Action(() => { MessageBox.Show(msg, name); });
Dispatcher.CurrentDispatcher.BeginInvoke(dg);

Anonymous methods and delegates

CS0120: An object reference is required for the nonstatic field, method, or property 'foo'

Community
  • 1
  • 1
he_the_great
  • 6,554
  • 2
  • 30
  • 28
3
Dispatcher.BeginInvoke(delegate() { MessageBox.Show("your stuff"); });
Gabriel
  • 377
  • 1
  • 3
  • 15
gayan1991
  • 753
  • 2
  • 11
  • 35
  • @gayan1991 Cannot convert anonymous method to delegate because it is not a delegate type... Does not work as proposed. – pzogr Aug 13 '16 at 14:01