繁体   English   中英

为什么我的自定义事件会引发异常?

[英]Why does my custom event throw an exception?

我正在编写一个记录用户空闲时间的程序,但是当我尝试运行该程序时,它将引发堆栈溢出异常。

这些是我的自定义事件

public void OnInactive(EventArgs e)
{
    this.OnInactive(new EventArgs());
    do
    {
        var idle2 = GetIdleTime();
        GetIdleTime();
        System.Diagnostics.Debug.WriteLine(idle2);
    }
    while (timer.Interval > 5000);
}

public void OnActive(EventArgs e)
{
    this.OnActive(new EventArgs());
    if (timer.Interval < 5000)
    {
        var idle3 = GetIdleTime();
        System.Diagnostics.Debug.WriteLine(idle3);
    }
}

我已经断点了代码,尝试查找问题的根源,它似乎位于this.OnInactive(new EventArgs()); ,但是由于我是Custom Events的初学者,而且很长时间没有使用C#编写代码,因此我对如何解决此问题感到非常困惑。

任何和所有与这个问题的帮助将不胜感激!

在此先感谢=]

您的处理程序方法在输入时立即调用自身:

this.OnInactive(new EventArgs());

这导致一系列调用:

OnInactive-> OnInactive-> OnInactive-> ...->

这将一直持续到您用尽堆栈空间并且运行时抛出StackOverflowException为止。

目前尚不清楚您要通过递归调用实现的目标,但是您应该可以删除它。

您的OnActive处理程序中存在相同的问题。

编辑:为了回应这些评论,似乎您正在尝试在方法开始时引发事件本身。 假设您的事件声明如下所示:

public event EventHandler InActive;

那么您可以使用以下方法提高它:

EventHandler inactiveEvent = this.InActive;
if(inactiveEvent != null)
{
    inactiveEvent(this, e);
}

同样适用于您的Active事件。

我客串你试图调用基方法,但事实上,你现在叫OnInactive击球时OnInactive 此行为是递归的,并且由于StackOverflow exception而最终将停止。

您可以使用base.<function name>调用基本函数。 例如:

class SpecialDerived : Base
{
    public override void Say()
    {
        Console.WriteLine("Called from Special Derived.");
        base.Say();
    }
}

更多信息: http : //msdn.microsoft.com/zh-cn/library/hfw7t1ce(v=vs.71).aspx

这些不是事件处理程序,而是为了引发活动和非活动事件而要调用的方法– Reece Cottam

您需要实际调用该事件。

public class ReecesWatcher
{
    public event EventHandler ActiveEvent;
    public event EventHandler InactiveEvent;


    protected virtual void OnInactive(EventArgs e)
    {
        // Fire the event using the () syntax. Fire it through
        // a test variable so that we can reliabilty test for null, 
        // if there are no subscribers.
        EventHandler inactiveEventTest = InactiveEvent;
        if (inactiveEventTest != null)
        {
            inactiveEventTest(this, new EventArgs());
        }

        do
        {
            var idle2 = GetIdleTime();
            GetIdleTime();
            System.Diagnostics.Debug.WriteLine(idle2);
        }
        while (timer.Interval > 5000);
    }

    protected virtual void OnActive(EventArgs e)
    {
        // Fire the event using the () syntax. Fire it through
        // a test variable so that we can reliabilty test for null, 
        // if there are no subscribers.
        EventHandler activeEventTest = ActiveEvent;
        if (activeEventTest != null)
        {
            activeEventTest(this, new EventArgs());
        }

        if (timer.Interval < 5000)
        {
            var idle3 = GetIdleTime();
            System.Diagnostics.Debug.WriteLine(idle3);
        }
    }

    // ... the rest of your class, where you call OnActive and OnInactive to 
    // cause the events to be fired.
}

我建议不要公开您的OnActive和OnInactive方法,否则,您会将过多的实现暴露给程序的其余部分。 如果您希望从该类继承该类,请对其进行保护,否则我通常将它们完全私有,因为它们基本上是该类其余部分调用的包装函数。

我认为您需要对事件有更多的了解。 让我通过示例代码进行解释。

Class A{
   public event  OnInactive;
   public event  OnActive;
}

当classA中发生任何更改时,您想更新ClassB中的内容。 因此,您将在ClassB中实现A类的事件。

链接将详细描述您。

我的理解是,当您从同一个类触发事件并在同一个类中侦听时,不会使用任何事件。

暂无
暂无

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

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