Jedhi Posted January 23, 2006 Posted January 23, 2006 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 ? Quote
Igor Sukhov Posted January 24, 2006 Posted January 24, 2006 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: 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(); } } Quote
Jedhi Posted January 25, 2006 Author Posted January 25, 2006 Thank you so much. Your help has really been a big help for me. Thank you so much. Quote
Joe Mamma Posted January 29, 2006 Posted January 29, 2006 (edited) You may want to consider something like this - 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: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(); } Edited January 29, 2006 by Joe Mamma Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.