Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Event Notification
Message
From
01/12/2010 21:32:08
 
 
To
01/12/2010 16:31:42
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 4.0
Application:
Desktop
Miscellaneous
Thread ID:
01491399
Message ID:
01491502
Views:
56
>>>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.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform