MultiDispatchCommand (cont'd)

Apr 4, 2008 at 12:00 AM
There is also the Execute that leaves be perplex:

Existing:
public virtual void Execute(object parameter)
{
    Queue<ICommand> commands = new Queue<ICommand>(registeredCommands);
 
    while (commands.Count > 0)
    {
        ICommand command = commands.Dequeue();
        if (ShouldExecute(command))
            command.Execute(parameter);
    }
}

Proposed:
public virtual void Execute(object parameter)
{
  foreach (var command in registeredCommands.Where(ShouldExecute))
  {
    command.Execute(parameter);
  }
}

What's with the Queue?
Apr 4, 2008 at 7:48 PM


francois_tanguay wrote:

What's with the Queue?



Could it be a technique used to support multithreading? I noted there are static instances of MultiDispatchCommand...
Apr 4, 2008 at 9:53 PM

BillKrat wrote:
Could it be a technique used to support multithreading? I noted there are static instances of MultiDispatchCommand...

If so, I think it's still flawed because the Queue<T> ctor doesn't do any locking.
Apr 4, 2008 at 11:56 PM
Exactly, Queue isn't thread safe and from what I see, I don't see any logical reasons to push the list into a queue and then dequeue it.
Apr 5, 2008 at 12:27 AM


francois_tanguay wrote:
Exactly, Queue isn't thread safe and from what I see, I don't see any logical reasons to push the list into a queue and then dequeue it.


Understood, however not having tried it my assumption was that since they are creating a local instance, and using an enumerator as a parameter (as a result "the elements are copied into Queue(T) in the same order they are read by the IEnumerator(T) of the collection") then it's collection won't have a risk of being changed - thus the basis for my question; I have read much on the topic but do not have any experience (yet).

Apr 13, 2008 at 8:34 PM
The idea behind the queue is that we want to have a snapshot of all the registered commands at one point in time (specifically when you call Execute on the dispatcher command). The registered commands' list could be changed as a consequence of executing a particular command, so it's not enough to just iterate through the list of commands and call Execute on each.
Anyway, we didn't take into account that the Queue ctor may not be thread safe, so we may need a lock or something of the sort.

Thank you for your feedback.
Julian Dominguez