Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Event Notification
Message
From
02/12/2010 10:12:44
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
01/12/2010 21:32:08
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 4.0
Application:
Desktop
Miscellaneous
Thread ID:
01491399
Message ID:
01491565
Views:
54
>>>>OK, thanks to all.. Just to be sure I understand, if I subscribe to the exact same event and wire it to the exact same handler more than once, it would be basically subscribed twice. And if I unsubscribe it once, then it would still be subscribed because it was subscribed more than the one time.
>>>>
>>>
>>>Yes. Just understand that if it's hooked up more than once it will get called once for each subscription.
>>
>>Ugh! Not what I would want unless it only gets called once if only raised by one class that raised it. If that is the case, I will need to do some work in my method to prevent it from doing more than I want.
>>Thanks
>>Tim
>
>Picky little detail... it's per object not per class.
>
>Here's a quick and dirty little piece of console code that shows it:
>
>
>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();
>      }
>    }
>  }
>
>
>Not sure what your code is looking like but if you toss the above into a console app you can play with it.
>
>Also, since this is WPF... you might want to take a look at weak events.

Hi John,

I will have to look at Weak Events as I have not heard that term. Your sample helped although I modified it to be a more exact simulation. Here is what I did and I get the results I want from this. Perfect. Thanks!
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;
		}
	}


}
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform