YATB (Yet Another Tech Blog)

C#’s event handling takes event handling notification on a twist from Java’s tried-and-true broadcast-subscription mechanism. Using events, disguised in the form of delegates, event subscribers can similarly subscribe to event notification. The event source notifies all event subscribers to calling back on the delegate method they initially sent for subscribing to the event notification list.

  1. using System;
  2. using System.Threading;
  3.  
  4. namespace TestEvents
  5. {
  6.     public class WakeupEventArgs : EventArgs
  7.     {
  8.         // Information to be distributed from the event source
  9.         // to each event subscriber (sink)
  10.         private DateTime WakeupDateTime_;
  11.  
  12.         public WakeupEventArgs(DateTime dt)
  13.         {
  14.             WakeupDateTime_ = dt;
  15.         }
  16.  
  17.         public string GetWakeupDateTimeStr { 
  18.             get { return WakeupDateTime_.ToShortTimeString(); }
  19.         }
  20.     }
  21.  
  22.  
  23.     public class Context
  24.     {
  25.         // context object- usually the server application-
  26.         // or any object that controls the notification of events.
  27.  
  28.         // publicly declare the signature of the event callback method
  29.         // any object that wants to be notified of an event should
  30.         // have a method prepared with just this method signature
  31.         public delegate void WakeupEventHandler(
  32.             Object sender, WakeupEventArgs e);
  33.  
  34.         // this event is used to maintain a list of observer delegates
  35.         // and raise an event when a poll completes
  36.         private event WakeupEventHandler WakeupEvent;
  37.  
  38.         public Context()
  39.         {
  40.         }
  41.  
  42.         public void AddToEventNotificationChain(WakeupEventHandler h)
  43.         {
  44.             WakeupEvent += h;
  45.         }
  46.        
  47.  
  48.         public void Sleep()
  49.         {
  50.             try {
  51.                 Thread.Sleep(10000);
  52.                 WakeupEvent(this,new WakeupEventArgs(DateTime.Now));
  53.             } catch ( Exception )
  54.             {}
  55.         }
  56.     }
  57.  
  58.  
  59.     public class Client
  60.     {
  61.         public Client()
  62.         {
  63.         }
  64.  
  65.         public void SubscribeToContentEvent(Context c)
  66.         {
  67.             c.AddToEventNotificationChain(this.WakeupEventHandler);
  68.         }
  69.        
  70.         public void WakeupEventHandler(Object sender, WakeupEventArgs e)
  71.         {
  72.             Console.Out.WriteLine(e.GetWakeupDateTimeStr);
  73.         }
  74.  
  75.     }
  76.  
  77.  
  78.     class Program
  79.     {
  80.         static void Main(string[] args)
  81.         {
  82.             Context ct = new Context();
  83.             Client c = new Client();
  84.             c.SubscribeToContentEvent(ct);
  85.  
  86.             ct.Sleep();
  87.             // the context object will sleep for 10 seconds,
  88.             // then notify the client
  89.             // the client will print wake up time to console.
  90.         }
  91.     }
  92. }
§231 · December 2, 2009 · C# · · [Print]

Leave a Reply

You must be logged in to post a comment.