Dispatcher.BeginInvoke问题 [英] Dispatcher.BeginInvoke problems

查看:582
本文介绍了Dispatcher.BeginInvoke问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了非静态字段,方法或属性'System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)'的对象引用。

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));
                }
            });

        }
    }
}


推荐答案

该错误表明您需要 Dispatcher 实例来调用 BeginInvoke ,因为它是实例方法。您在哪里获得该实例取决于您要在哪里调度呼叫。

The error indicates that you need an instance of Dispatcher to call BeginInvoke since it is an instance method. Where you get that instance depends on where you want to dispatch a call.

也许您可以尝试使用静态属性 Dispatcher.CurrentDispatcher 来获取当前线程的调度程序实例然后在该实例上调用 BeginInvoke 。要么以某种方式从您要调用的特定线程将调度程序实例获取到您的方法。

Perhaps you could try using the static property Dispatcher.CurrentDispatcher to get the instance of the dispatcher for the current thread and then call BeginInvoke on that instance. Either that or somehow get a dispatcher instance to your method from the particular thread you want to call to.

这篇关于Dispatcher.BeginInvoke问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆