Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 ?

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

Posted (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 by Joe Mamma

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...