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));
}
});
}
}
}
BeginInvoke(DispatcherPriority, Delegate, Object) Executes the specified delegate asynchronously at the specified priority and with the specified argument on the thread the Dispatcher is associated with.
Invoke(Action, DispatcherPriority, CancellationToken)Executes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with.
The Dispatcher maintains a prioritized queue of work items for a specific thread. When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down.
WPF Dispatcher is associated with the UI thread. The UI thread queues methods call inside the Dispatcher object. Whenever your changes the screen or any event executes, or call a method in the code-behind all this happen in the UI thread and UI thread queue the called method into the Dispatcher queue.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With