views:

118

answers:

4

Hi,

I have a class which provides a custom event:

public delegate void ResultEvent(bool result);

public class Service : INotifyPropertyChanged
{
    public event ResultEvent Result;
}

Two other objects have a refernce to this like this:

public partial class SomeRandomClass
{
    private Service service;

    public SomeRandomClass()
    {
        this.InitializeComponent();

        service = new Service();
        service.Result += new ResultEvent(service_Result);
    }
}

The problem is, only the last object which creates a new object of "Service" seems to recognize if the event is dispatched. If I do something with the "Service" on previoulsy generated objects, so that the event has to be dispatched, the event will be raised in the "Service"-object, but the handler will not be called.

Anybody has an idea what may be my problem?

A: 

The ResultEvent delegate is not static - each instance of SomeRandomClass will have its own instance of Service and it's own delegate registered to that Service instance's event, and not any other SomeRandomClass's service instance.

I don't really get what you're asking; is this helpful?

thecoop
your're absolutely right, so if I create 10 instances of "Service" in 10 different objects, and subscribe to the event in each object, each object has to handle its own instance and events of "Service". But this is not the case. Only the last object I create will handle its Service event, all the others don't care if their Service dispatches the event.
Thomas
+1  A: 

It sounds like you've got a static reference somewhere which may be overwritten each time a new instance is being created, but there's nothing like that showing in the code you've provided so far. You do want each Service instance to have its own separate set of handlers, right?

Could you post a short but complete program which demonstrates the problem? Basically rip out all the "real" code until all you've got is the event handling and firing. (You can make the event firing something like a timer.)

Jon Skeet
I wish I could post the whole code, but I'm not allowed, thats why I use examples.
Thomas
@Thomas examples are good but they need to exhibit the issue your having. Your example unfortunately do not do that.
Rune FS
@Thomas: You don't have to post the real code - and indeed posting a huge chunk of code which is unrelated to the problem wouldn't be useful. However, that doesn't stop you from trying to reproduce the problem in a short but complete program.
Jon Skeet
A: 

This is not a ton of information, however this is what it seems like to me. Service is an instance variable inside SomeRandomClass so if you are losing reference to SomeRandomClass then it get's garbage collected and the event never fires.

However it would be helpful if you posted a full example.

To demonstrate what I am talking about consider the following example.

SomeRandomClass x;
for(int i =0; i< 10; i++)
{
  x = new SomeRandomClass(); //this will subscribe to only the last isntance
}
Stan R.
A: 

There's a missing incomplete code, where's the event to raise on the 'PropertyChanged' event despite it is implementing INotifyPropertyChanged?

You are also missing a means for the Result event to be raised... I have amended the code to suit your purposes...IMHO, you should be using the standard Event parameters when wiring up the event handler as that is the general pattern used throughout the Framework...

service.Result += new ResultEvent(service_Result);

private void service_Result(object sender, EventArgs e){
   ....
}

I have modified the code below to complete it for the sake of completion...

public class Service : INotifyPropertyChanged
{
    public delegate void ResultEvent(bool result);
    public event ResultEvent Result;

    // Where's the raise property changed event handler?

    private void OnServiceResult(bool result){
        if (this.Result != null) this.Result(result);
    }
}

Hope this helps, Best regards, Tom.

tommieb75
The evil wizball magician stole the colorz from ur codez :-(
herzmeister der welten
@herzmeister: What? I don't answer in textspeak here on this forum! :)
tommieb75
are you sure mr t0mm13b? :-> ... I just meant your code sample is not formatted with those beautiful colors. Someone ate your `<code>` tags.
herzmeister der welten
@Herzmeister: Ahhh....I don't use the toolbar to do that for me...I use the code tags... :P :D lol....
tommieb75