Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatcher.BeginInvoke problems

Tags:

c#

dispatcher

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

        }
    }
}
like image 445
cmaduro Avatar asked Apr 08 '10 00:04

cmaduro


People also ask

What is dispatcher BeginInvoke?

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.

What does Dispatcher Invoke do?

Invoke(Action, DispatcherPriority, CancellationToken)Executes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with.

Why we use Dispatcher in c#?

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.

How WPF Dispatcher works?

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.


1 Answers

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

like image 50
Abhishek Oza Avatar answered Oct 14 '22 22:10

Abhishek Oza