Dispatcher.BeginInvoke 问题

发布于 2024-08-28 13:47:12 字数 948 浏览 7 评论 0原文

我收到此代码的“非静态字段、方法或属性‘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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-09-04 13:47:12

自从发布这个问题的最后一个答案以来,情况发生了一些变化。
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 now Deployment.Current.Dispatcher.BeginInvoke

羅雙樹 2024-09-04 13:47:12

该错误表明您需要一个 Dispatcher 实例来调用 BeginInvoke,因为它是一个实例方法。从哪里获取该实例取决于您想要将呼叫分派到哪里。

也许您可以尝试使用静态属性 Dispatcher.CurrentDispatcher 获取当前线程的调度程序实例,然后在该实例上调用 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文