开发者

c# event handling

I have a class which provides a custom event:

public delegate void ResultEvent(bool result);

开发者_C百科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?


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.)


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?


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜