sashomaga

Subscribe to event

Mar 7th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5.  
  6. // Define a class to hold custom event info
  7. public class CustomEventArgs : EventArgs
  8. {
  9.     public CustomEventArgs(string s)
  10.     {
  11.         info = s;
  12.     }
  13.     private string info;
  14.  
  15.     public string Info
  16.     {
  17.         get { return info; }
  18.         set { info = value; }
  19.     }
  20. }
  21.  
  22. // Class that publishes an event
  23. class Publisher
  24. {
  25.  
  26.     //Event that using above class
  27.     public event EventHandler<CustomEventArgs> RaiseCustomEvent;
  28.  
  29.     public void DoSomething()
  30.     {
  31.         // Write some code that does something useful here
  32.         // then raise the event. You can also raise an event
  33.         // before you execute a block of code.
  34.        
  35.         OnRaiseCustomEvent(new CustomEventArgs("Help!!!"));
  36.         Thread.Sleep(2000);
  37.         //recursion
  38.         DoSomething();
  39.     }
  40.  
  41.     // Wrap event invocations inside a protected virtual method
  42.     // to allow derived classes to override the event invocation behavior
  43.     protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
  44.     {
  45.         // Make a temporary copy of the event to avoid possibility of
  46.         // a race condition if the last subscriber unsubscribes
  47.         // immediately after the null check and before the event is raised.
  48.         EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
  49.  
  50.         // Event will be null if there are no subscribers
  51.         if (handler != null)
  52.         {
  53.            
  54.            
  55.             e.Info += String.Format(" at {0}", DateTime.Now.ToString());
  56.             // Use the () operator to raise the event.
  57.             handler(this, e);
  58.         }
  59.     }
  60. }
  61.  
  62.  
  63. class Subscriber
  64. {
  65.     private string id;
  66.     public Subscriber(string ID, Publisher pub)
  67.     {
  68.         id = ID;
  69.         // Subscribe to the event
  70.         pub.RaiseCustomEvent += HandleCustomEvent;
  71.     }
  72.  
  73.     // Define what actions to take when the event is raised.
  74.     void HandleCustomEvent(object sender, CustomEventArgs e)
  75.     {
  76.         Console.Beep();
  77.         Console.WriteLine("I caught the event!!!");
  78.         Console.WriteLine(e.Info);
  79.     }
  80. }
  81.  
  82. class Program
  83. {
  84.     static void Main()
  85.     {
  86.         Publisher pub = new Publisher();
  87.         Subscriber sub1 = new Subscriber("sub1", pub);
  88.        
  89.         // Call the method that raises the event.
  90.         pub.DoSomething();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment