4
votes
void GetResponseCallback(IAsyncResult asynchronousResult)
        {

            try
            {
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();

                XmlReader xmlDoc = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(responseString)));

                while (xmlDoc.Read())
                {
                    if (xmlDoc.NodeType == XmlNodeType.Element)
                    {

                        if (xmlDoc.Name.Equals("ResponseCode"))
                        {
                            responseCode = xmlDoc.ReadInnerXml();

                        }

                    }

                }
                if (Convert.ToInt32(responseCode) == 200)
                {

                   MessageBox.Show("Success");
                }


                // Close the stream object
                streamResponse.Close();
                streamRead.Close();
                // Release the HttpWebResponse
                response.Close();


            }
            catch (WebException e)
            {
                // Error treatment
                // ...
            }
        }

in above code Messagebox.show dispalying "Invalid cross thread access".please tell me how to resolve this...

2

2 Answers

8
votes
    Dispatcher.BeginInvoke(() =>
            MessageBox.Show("Your message")
    );
6
votes

Any interaction with the UI from code needs to be on the dispatcher thread, your callback from the HTTP request will not be running on this thread hence the error.

You should be able to use something like

Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.SHow("Success") );

to display the message box

HTH - Rupert.