简体   繁体   中英

C# Events handling problem for base and derived classes

I have this case where I'm creating 2 different event handlers placed in a base class and subscribing to them accordingly from Quotes and Charts classes. Problem I'm having is that the first subscription triggers fine for the first event but any following subscriptions don't get executed. I have included an example of 2 different handlers, Quotes and Charts, Quotes executes first time with no problems, but Charts does not trigger when data is received.

Base Class:

public abstract class MyBaseClass
{
    protected virtual void RaiseOnQuoteData(string item) { }
    protected virtual void RaiseOnChartData(string item) { }

    void OnDataReceived(object sender, DataEventArgs e)
    {
        if (e.Item == "QUOTE")
            RaiseOnQuoteData(e.Item);
        else if (e.Item == "CHART")
            RaiseOnChartData(e.Item);
    }
}

Quote and Chart Classes:

public class Quote : MyBaseClass
{
    public event EventHandler<DataEventArgs<quoteRecord>> OnQuoteData;

    protected override void RaiseOnQuoteData(string item)
    {
        OnQuoteData.Raise<DataEventArgs<quoteRecord>>(this, new DataEventArgs<quoteRecord>(item));
    }
}

public class Chart : MyBaseClass
{
    public event EventHandler<DataEventArgs<chartRecord>> OnChartData;

    protected override void RaiseOnChartData(string item)
    {
        OnChartData.Raise<DataEventArgs<chartRecord>>(this, new DataEventArgs<chartRecord>(item));
    }
}

Subscription:

public class QuoteSubscription
{
    public static void SubscribetoQuoteData()
    {
        Quote Q = new Quote();
        Q.OnQuoteData += new EventHandler<DataEventArgs<quoteRecord>>(q_OnQuoteData);
    }

    static void q_OnQuoteData()
    { 
        //Executes fine
    }
}

public class ChartSubscription
{
    public static void SubscribetoChartData()
    {
        Chart C = new Chart();
        C.OnChartData += new EventHandler<DataEventArgs<chartRecord>>(q_OnChartData);
    }

    static void q_OnChartData()
    {
        //Does not execute
    }
}

This is implemented in ASP.NET 4.0, Is there any chance that instantiating the derived classes could be the problem since both classes do share the same base class? Any help pointing to the cause would be greatly appreciated.

What is there in Raise? This must be an extension method, since EventHandler per se doesn't define such a method. Therefore, you can put a breakpoint inside and see, what's going on. (And you could perhaps put a breakpoint inside RaiseOnChartData as well.)

Could it be that you are creating the object within the method scope and has gone out of scope. You may get the first message by coincidence just because it hasn't been GC-ed. Try creating the quote and chart objects as static class member object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM