>using System; >using System.Collections.Generic; >using System.Linq; >using System.Text; > >namespace eventTest > { > > public class MyPublisher > { > public event EventHandler RaiseCustomEvent; > > public void DoEvent() > { > EventHandler handler = RaiseCustomEvent; > if (handler != null) > { > handler(this,new EventArgs() ); > } > } > } > > public class MySubscriber > { > private MyPublisher _Pub; > > public string Name { get; set; } > > public MySubscriber(MyPublisher pub, string name) > { > _Pub = pub; > Name = name; > _Pub.RaiseCustomEvent += new EventHandler(_Pub_RaiseCustomEvent); > _Pub.RaiseCustomEvent += new EventHandler(_Pub_RaiseCustomEvent); > } > > void _Pub_RaiseCustomEvent(object sender, EventArgs e) > { > Console.WriteLine("Event in "+Name); > } > > public void UnSub() > { > _Pub.RaiseCustomEvent -= new EventHandler(_Pub_RaiseCustomEvent); > } > } > > class Program > { > static void Main(string[] args) > { > MyPublisher pub = new MyPublisher(); > Console.WriteLine("Start..."); > MySubscriber sub1 = new MySubscriber(pub, "Sub 1"); > MySubscriber sub2 = new MySubscriber(pub, "Sub 2"); > pub.DoEvent(); > // 4 calls to event handler in subscriber > Console.WriteLine("UnSub..."); > sub2.UnSub(); // Single unsubscribe from event in sub2 > pub.DoEvent(); > // 3 calls to event handler in subscriber > Console.ReadLine(); > } > } > } >>
using System; namespace EventsSubscribingConsole { class Program { static void Main(string[] args) { // Similate getting the MainForm View Running MySubscriber mainSub = new MySubscriber(); Console.WriteLine("Started MainForm Subscriber"); // Have the Main Sub do its work mainSub.DoSimulation(); Console.ReadLine(); } } // Subscriber simulates my Main View Model public class MySubscriber { public void DoSimulation() { // Start up First Publisher MyPublisher PubA = new MyPublisher("Publisher A"); PubA.CustomEvent += new CustomEventDelegate(Pub_CustomEventHandler); // Tell the Publisher to fire the event PubA.DoEvent(); // Start up Second Publisher MyPublisher PubB = new MyPublisher("Publisher B"); PubB.CustomEvent += new CustomEventDelegate(Pub_CustomEventHandler); // Tell the Second publisher to fire the event PubB.DoEvent(); Console.WriteLine("Fire Event again in PubA"); PubA.DoEvent(); // Remove First Publisher Console.WriteLine("Removing Publisher A"); PubA.CustomEvent -= new CustomEventDelegate(Pub_CustomEventHandler); PubA = null; Console.WriteLine("Publisher A Removed"); // Tell the Second Publisher to fire the event again. PubB.DoEvent(); } // Event Handler public void Pub_CustomEventHandler(object sender, CustomEventArgs e) { Console.WriteLine("Event in " + e.MyName); } } // Publisher that fires the event public class MyPublisher { public event CustomEventDelegate CustomEvent; public string PubName; public MyPublisher(string pubName) { PubName = pubName; Console.WriteLine("Publisher Created " + pubName); } /// <summary> /// Raises the Event /// </summary> public void DoEvent() { CustomEventDelegate handler = CustomEvent; if (handler != null) { handler(this, new CustomEventArgs(PubName)); } } } public delegate void CustomEventDelegate(object sender, CustomEventArgs e); public class CustomEventArgs : EventArgs { public string MyName { get; set; } public CustomEventArgs(string myName) { MyName = myName; } } }