Skip to main content

Posts

Showing posts from May, 2013

Publish Subscribe C#

This is a class which holds the information about the event.        public class TimeEventArgs : EventArgs     {         public TimeEventArgs( string currentTime)         {             this .CurrentTime = currentTime;         }         public string CurrentTime;     } This is a Subject class (Publisher) that the other classes will observe. So it publishes a handler so that other observer classes can subscribe to it.     public class Clock     {         public delegate void SecondChangeHandler ( object clock, TimeEventArgs e);         public SecondChangeHandler OnSecondChange;         public void RunClock()         {             for ( int i = 0; i <= 4; i++)             {                  Thread .Sleep(1000);                 TimeEventArgs te = new       TimeEventArgs ( DateTime .Now.ToString( "HH:mm:ss" ));                 if (OnSecondChange != null )                 {