我试图从异步调用写入表单。我在网上看到的示例表明这应该可以工作,但我一直收到错误消息。
首先,尽管 Disbatcher 从不调用 p 进行的调用。其次,我在调用 req.EndGetResponse(a); 时收到 System.Security.SecurityException;
什么可能导致问题?
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";
}
}