Im trying to write to a form from an asynchronous call. The examples I seen on line show that this should work but I keep getting an error.
First the call that is made though the Disbatcher never calls p. Second i get a System.Security.SecurityException on the call req.EndGetResponse(a);
What could be causing the problem?
public partial class Page : UserControl
{
Uri url = new Uri("http://www.google.com", UriKind.Absolute);
HttpWebRequest req;
private delegate void PrintToUIThread(string text);
PrintToUIThread p;
public Page()
{
InitializeComponent();
req = (HttpWebRequest)WebRequest.Create(url);
p = new PrintToUIThread(print);
req.BeginGetResponse(new AsyncCallback(WebComplete), req);
}
void WebComplete(IAsyncResult a)
{
try
{
Dispatcher.BeginInvoke(p);
HttpWebRequest req = (HttpWebRequest)a.AsyncState;
HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
Dispatcher.BeginInvoke(p);
}
catch (Exception ex)
{
print(ex.ToString());
}
}
private void print(string text)
{
PageTextBox.Text = text;
}
private void print()
{
PageTextBox.Text = "Call From Invoke";
}
}