2
votes

I am asking the user of my app per the XNA GamerService dialog-box, if he really wants to delete a specific product.

And if he presses yes, this will take action:

private void OnMessageBoxAction(IAsyncResult ar)
        {
            int? selectedButton = Guide.EndShowMessageBox(ar);
            switch (selectedButton)
            {
                case 0:
                    WebClient cweight = new WebClient();
                    cweight.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");

                    cweight.Credentials = new NetworkCredential(op.username, op.userpass);
                    cweight.DownloadStringCompleted += new DownloadStringCompletedEventHandler(deleted);
                    cweight.DownloadStringAsync(new Uri("http://mydomain.com"));
                    break;

                case 1:
                    Debug.WriteLine("1 pressed");
                    break;

                default:
                    Debug.WriteLine("default pressed");
                    break;
            }
        }

and when the download completes, I invoke the login method:

private void deleted(object sender, DownloadStringCompletedEventArgs e)
        {
            Debug.WriteLine("\n[#] deleted");

            if (e.Error != null)
            {
                Debug.WriteLine("Delete problem");
            }

            Debug.WriteLine("Delete successful");
            login(null, null);
        }

later on at login I get the invalid cross-thread access at globalprogress.Visibility = System.Windows.Visibility.Visible; and I am pretty sure, that that error would occur through the whole login method.

1
Deployment.Current.Dispatcher.BeginInvoke(() => { login(null, null); }); Did the trick. This way, the Dispatcher will execute that part of the code, instead of the background-thread. - IMX

1 Answers

0
votes

Handy class:

public class SmartDispatcher
{
    public static void BeginInvoke(Action action)
    {
        if (Deployment.Current.Dispatcher.CheckAccess()
            || DesignerProperties.IsInDesignTool)
        {
            action();
        }
        else
        {
            Deployment.Current.Dispatcher.BeginInvoke(action);
        }
    }
}