Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: C#  |  size: 1.52 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Hades.Utilities
  7. {
  8.         public class BaseMessage
  9.         {
  10.         }
  11.         public class MessageDispatcher
  12.         {
  13.                 protected class MessageHandler<T> where T : BaseMessage
  14.                 {
  15.                         public int ThreadID;
  16.                         public string MessageName;
  17.                         public Action<T> HandlerDelegate;
  18.                         public Queue<T> Messages = new Queue<T>();
  19.                 }
  20.  
  21.                 protected static System.Collections.Hashtable htHandlers = new System.Collections.Hashtable();
  22.                 public static bool RegisterHandler<T>(int threadId, string msgName, Action<T> dele) where T : BaseMessage
  23.                 {
  24.                         if (htHandlers.ContainsKey(msgName))
  25.                                 return false;
  26.  
  27.                         htHandlers.Add(msgName, new MessageHandler<T>() { ThreadID = threadId, MessageName = msgName, HandlerDelegate = dele });
  28.                         return true;
  29.                 }
  30.  
  31.                 public static bool SendMessage<T>(string msgName, T msg) where T : BaseMessage
  32.                 {
  33.                         var info = htHandlers[msgName] as MessageHandler<T>;
  34.  
  35.                         if (info == null)
  36.                                 return false;
  37.  
  38.                         lock (info.Messages)
  39.                         {
  40.                                 info.Messages.Enqueue(msg);
  41.                         }
  42.  
  43.                         return true;
  44.                 }
  45.  
  46.                 public static void Dispatch(int ThreadID)
  47.                 {
  48.                         foreach (MessageHandler<BaseMessage> info in htHandlers)
  49.                         {
  50.                                 if (info.ThreadID != ThreadID)
  51.                                         continue;
  52.  
  53.                                 Queue<BaseMessage> msgs = new Queue<BaseMessage>();
  54.                                 lock (info.Messages)
  55.                                 {
  56.                                         while (info.Messages.Count > 0)
  57.                                                 msgs.Enqueue(info.Messages.Dequeue());
  58.                                 }
  59.  
  60.                                 while (msgs.Count > 0)
  61.                                         info.HandlerDelegate(msgs.Dequeue());
  62.                         }
  63.                 }
  64.         }
  65. }