Advertisement
konalisp

messager.cs

Mar 5th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7.     public static void Main()
  8.     {
  9.         Message m1 = new Message { reciver = "one", fun = () => { Console.WriteLine("Say this"); } };
  10.         Messager.Send(m1);
  11.         var ms = Messager.Get("one");
  12.         foreach (var m in ms)
  13.         {
  14.             m.fun();
  15.         }
  16.     }
  17. }
  18.  
  19. public class Message
  20. {
  21.     public String reciver;
  22.     public Action fun;
  23. }
  24.  
  25. public static class Messager
  26. {
  27.     private static object locker = new Object();
  28.     private static List<Message> Queue = new List<Message>();
  29.    
  30.     public static List<Message> Get(String name)
  31.     {
  32.         List<Message> retval = new List<Message>();
  33.         lock (locker)
  34.         {
  35.             foreach (var m in Queue)
  36.             {
  37.                 if (m.reciver == name)
  38.                 {
  39.                     retval.Add(m);
  40.                 }
  41.             }
  42.             Queue = Queue.Except(retval).ToList();
  43.         }
  44.         return retval;
  45.     }
  46.    
  47.     public static void Send(Message m)
  48.     {
  49.         lock (locker)
  50.         {
  51.             Queue.Add(m);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement