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));
}
});
}
}
}
The error indicates that you need an instance of
Dispatcherto callBeginInvokesince 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.CurrentDispatcherto get the instance of the dispatcher for the current thread and then callBeginInvokeon that instance. Either that or somehow get a dispatcher instance to your method from the particular thread you want to call to.