Hi eberybody,
I have this piece of code in my Silverlight project:
private void button1_Click(object sender, RoutedEventArgs e)
{
string baseUri = "http://foo.bar";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(baseUri));
request.BeginGetResponse(new AsyncCallback(ReadCallback),request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
{
string resultString = streamReader1.ReadToEnd();
MessageBox.Show(resultString);
}
}
When I run the code, I get the following exception:
unauthorizedaccessexception
Invalid cross-thread access.
I thought It was a problem with the returned string. But, even if I put this:
MessageBox.Show("foobar");
the same exception rises. I think that the problem comes from the ReadCallback function that cannot perform such actions.
Can you help me?
Thank you,
Regards