为什么我的自定义事件会抛出异常?

时间:2013-07-03 12:03:15

标签: c# events

我正在编写一个记录用户空闲时间的程序,但是当我尝试运行该程序时,它会抛出一个Stack Overflow异常。

这些是我的自定义活动

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的初学者,我对如何解决这个问题非常感兴趣事件并没有在C#编码很长时间。

非常感谢您对此问题的任何帮助!

先谢谢=]

4 个答案:

答案 0 :(得分:5)

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

this.OnInactive(new EventArgs());

这会导致一系列调用:

OnInactive - &gt; OnInactive - &gt; OnInactive - &gt; ... - &gt;

将继续运行,直到用完堆栈空间并且运行时抛出StackOverflowException

目前还不清楚你要通过递归调用实现什么,但你应该能够删除它。

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

编辑:在回复评论时,您似乎正试图在方法开头提升事件本身。假设您的事件声明如下:

public event EventHandler InActive;

然后你可以用:

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

,同样适用于您的Active活动。

答案 1 :(得分:0)

我猜你试图调用基本方法,但实际上你在点击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/en-us/library/hfw7t1ce(v=vs.71).aspx

答案 2 :(得分:0)

  

这些不是事件处理程序,这些是将要使用的方法   被调用以提高活动和非活动事件 - Reece   科塔姆

您需要实际调用该事件。

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方法,否则你将太多的实现暴露给你的程序的其余部分。如果你希望继承类,那么让它们受到保护,否则我通常将它们完全保密,因为它们基本上是由类的其余部分调用的包装函数。

答案 3 :(得分:0)

我认为你需要的是对事件的更多了解。让我通过示例代码解释相同的内容。

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

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

this链接会详细描述您。

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

相关问题