簡體   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