Simple Event Sample

using System;
 
namespace EventSample {
    class Program {
        static void Main(string[] args) {
            Metronome m = new Metronome();
            Listener l = new Listener();
            l.Subscribe(m);
            m.Start();
        }
    }
    public class Metronome {        
        public event EventHandler<EventArgs> OnTick;
        public EventArgs e = null;                
        public void Start() {
            while (true) {
                System.Threading.Thread.Sleep(3000);
                if (OnTick != null) {
                    OnTick(this, e);                    
                }
            }
        }
    }
    public class Listener {
        public void Subscribe(Metronome m) {            
            m.OnTick += HeardIt;
        }
        private void HeardIt(object sender, EventArgs e) {
            System.Console.WriteLine("HEARD IT");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *