Dispatcher.BeginInvoke problems

cmaduro picture cmaduro · Apr 8, 2010 · Viewed 20.4k times · Source

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

        }
    }
}

Answer

Abhishek Oza picture Abhishek Oza · Jan 25, 2012

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