Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is the difference of delegator and dispatcher?

I am not quite sure of either concept.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

Delegation is assigning a responsibility to some other object. For example, if I am invited to a meeting and I assign it to a junior colleague, I am delegating. The point to note here is that it is the junior colleague who will attend the meeting and there is no intermediary between him and me.

Dispatching is not delegating. Dispatcher is more like an intermediary who relays a request to someone else. For example, to attend the meeting my astute junior colleague may ask a cab company for a pick up and the guy receiving the request may dispatch it to a nearby cab driver. The point here is that dispatcher is only passing along the request.

share|improve this answer

Dispatcher

  • The dispatcher can be considered as a Queue where the events are sent.
  • A dispatcher runs on the UI thread and executes events for the UI.
  • In windows, UI controls may only be modified by the thread that created them, so any changes to the UI must be done from the UI thread - thus that is one of the critical reasons why operations that modify window elements must be sent to the UI's dispatcher.
  • A background thread that calls BeginInvoke on the dispatcher will return immediately even though the dispatcher may not have gotten around to processing. If Invoke had been used instead, the background thread would block until the UI thread completed processing. Note that in Silverlight, there is no Invoke on the Dispatcher and in most cases you probably don't want your background thread blocking while the UI thread is processing work.

Delegate

  • Delegate is used for Callback and Event Handling.
  • The Dictionary meaning of Delagate is "A person acting for another person". but in .net its a really means a method acting for another method.
  • Delegate uses worker threads in the thread pool.
  • When you're on the UI thread (or any thread really) you can call BeginInvoke and Invoke on a delegate. BeginInvoke will use a worker thread to call the delegate using the same way I have told above. Invoke, however, would not use a different thread. It would simply invoke the delegate synchronously in the context of the calling thread and return when completed.

Consider this: delegate.Invoke/BeginInvoke and this.dispatcher.Invoke/BeginInvoke.

  • BeginInvoke means: "Do this and return before it completes. I either don't care about the return value or you can call me back at this address at some point in the future."
  • Invoke means: "Do this and I'll sit here and wait for it to complete."

In short: The BeginInvoke is an asynchronous call. It executes a delegate asynchronously on the thread the Dispatcher is associated with.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.