Hashtable and Queue

Jedhi

Centurion
Joined
Oct 2, 2003
Messages
127
Let say that I have a Hashtable ht = new Hashtable()

My hashtable consist of a key and a value. The value is a Queue. Everytime a command is being saved for this key the queue will be increased. Until a command has been executed and it will there after be decreased.

Since I can only add a key to the Hashtable once, how do I then solve this problem ?
 
Jedhi said:
Let say that I have a Hashtable ht = new Hashtable()

My hashtable consist of a key and a value. The value is a Queue. Everytime a command is being saved for this key the queue will be increased. Until a command has been executed and it will there after be decreased.

Since I can only add a key to the Hashtable once, how do I then solve this problem ?

You need is to add condition check in your code - does hashtable already hold the value for the specific command or not. Have a look at the possible implementations of the Add and ExecuteCommand methods:


Code:
private IDictionary _commands = new Hashtable();

void AddCommand(Key key, Command command)
{
   Queue queue = null;  

   // Check - if there is already the queue of commands for the specific key.  
   //  
   if (_commands.Contains(key))
   {
        queue = (Queue)_commands[key];
   }
   else
   {
        queue = new Queue();
   } 

   queue.Enqueue(command);
   _commands[key] = queue;
}

        void ExecuteCommand(Key key)
        {
            Queue queue = null;

            // Check - if there is already the queue of commands for the specific key.  
            //  
            if (_commands.Contains(key))
            {
                queue = (Queue)_commands[key];
            }

            if (null == queue)
            {
                return;
            }
            else
            {
                Command command = (Command)queue.Dequeue();
                command.Execute();
            }
        }
 
You may want to consider something like this -
Code:
namespace KeyedQueuedCommand
{
public abstract class QueuedCommand 
{
	KeyedQueue _queue;
 
	public QueuedCommand(KeyedQueue queue)
	{
	 _queue = queue;
	 _queue.Enqueue(this);
	}
 
	protected abstract void OnExecute();
 
	public void Execute()
	{
	 this.OnExecute();
	 _queue.Dequeue();
	}
}
public class KeyedQueue:Queue
{
	static Hashtable _hash;
	object _key;
	static KeyedQueue()
	{
	 _hash = new Hashtable();
	}
 
	private KeyedQueue(object key)
	{
	 _key = key;
	 _hash[key] = this;
	}
 
	static public KeyedQueue GetKeyedQueue(object key)
	{
	 lock(_hash)
	 {
		if (_hash[key] == null)
		 new KeyedQueue(key);
	 }
	 return _hash[key] as KeyedQueue;
	}
 
	public override object Dequeue()
	{
	 object result = base.Dequeue();
	 Console.WriteLine("Object dequeud from Queue {0}\n{1} Object(s) remaining", 
			_key.ToString(), this.Count.ToString());
	 if (this.Count == 0)
	 {
		_hash.Remove(_key);
		Console.WriteLine("{0} queue removed", _key.ToString());
	 }
	 return result;
	}
 
	public static void Test()
	{
	 int i = 0;
	 foreach(object key in _hash.Keys)
	 {
		KeyedQueue q = _hash[key] as KeyedQueue;
		if ( q != null) i += q.Count; 
	 }
	 Console.WriteLine("Test - {0} commands left to execute", i.ToString()); 
	}
}
}

example usage:
Code:
public class DateCommand: KeyedQueuedCommand.QueuedCommand
{
	public DateCommand(KeyedQueuedCommand.KeyedQueue queue):base(queue){}
	protected override void OnExecute()
	{
		Console.WriteLine("Executed @ {0}", DateTime.Now.ToLongTimeString());
	}
}
 
private void button1_Click(object sender, System.EventArgs e)
{
	Random rand = new Random();
	ArrayList commandlist = new ArrayList();
	for (int i = 0; i < 100; i++)
	{
		commandlist.Add(new DateCommand(
			KeyedQueuedCommand.KeyedQueue.GetKeyedQueue(Math.Round(10 * rand.NextDouble()))));		 
	}
	
	foreach(KeyedQueuedCommand.QueuedCommand cmd in commandlist)
		cmd.Execute();
 
	KeyedQueuedCommand.KeyedQueue.Test();
}
 
Last edited:
Back
Top