简体   繁体   English

基类和派生类的C#事件处理问题

[英]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. 在这种情况下,我将创建两个放置在基类中的不同事件处理程序,并分别从Quotes和Charts类订阅它们。 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? 这是在ASP.NET 4.0中实现的,由于两个类确实共享相同的基类,是否有可能实例化派生的类? Any help pointing to the cause would be greatly appreciated. 指出原因的任何帮助将不胜感激。

What is there in Raise? Raise有什么? This must be an extension method, since EventHandler per se doesn't define such a method. 这必须是扩展方法,因为EventHandler本身并未定义这种方法。 Therefore, you can put a breakpoint inside and see, what's going on. 因此,您可以在其中放置一个断点,看看发生了什么。 (And you could perhaps put a breakpoint inside RaiseOnChartData as well.) (也许您也可以在RaiseOnChartData放置一个断点。)

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. 您可能偶然碰到了第一条消息,因为它没有经过GC编辑。 Try creating the quote and chart objects as static class member object 尝试将报价和图表对象创建为静态类成员对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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