Dispatcher.BeginInvoke 问题
我收到此代码的“非静态字段、方法或属性‘System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)’需要对象引用”。
private void ResponseCompleted(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
Dispatcher.BeginInvoke( () => {
try
{
XDocument resultsXml = XDocument.Load(sr);
QueryCompleted(new QueryCompletedEventArgs(resultsXml));
}
catch (XmlException e)
{
XDocument errorXml = new XDocument(new XElement("error", e.Message));
QueryCompleted(new QueryCompletedEventArgs(errorXml));
}
});
}
}
}
I'm getting "An object reference is required for the non-static field, method, or property 'System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)'" for this code.
private void ResponseCompleted(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
Dispatcher.BeginInvoke( () => {
try
{
XDocument resultsXml = XDocument.Load(sr);
QueryCompleted(new QueryCompletedEventArgs(resultsXml));
}
catch (XmlException e)
{
XDocument errorXml = new XDocument(new XElement("error", e.Message));
QueryCompleted(new QueryCompletedEventArgs(errorXml));
}
});
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自从发布这个问题的最后一个答案以来,情况发生了一些变化。
System.Windows.Threading.Dispatcher.BeginInvoke
现在是Deployment.Current.Dispatcher.BeginInvoke
Things have changed a bit since the last answer was posted for this question.
System.Windows.Threading.Dispatcher.BeginInvoke
is nowDeployment.Current.Dispatcher.BeginInvoke
该错误表明您需要一个
Dispatcher
实例来调用BeginInvoke
,因为它是一个实例方法。从哪里获取该实例取决于您想要将呼叫分派到哪里。也许您可以尝试使用静态属性
Dispatcher.CurrentDispatcher
获取当前线程的调度程序实例,然后在该实例上调用BeginInvoke
。或者以某种方式从您想要调用的特定线程获取一个调度程序实例到您的方法。The error indicates that you need an instance of
Dispatcher
to callBeginInvoke
since it is an instance method. Where you get that instance depends on where you want to dispatch a call.Perhaps you could try using the static property
Dispatcher.CurrentDispatcher
to get the instance of the dispatcher for the current thread and then callBeginInvoke
on that instance. Either that or somehow get a dispatcher instance to your method from the particular thread you want to call to.