C# .WriteLine(idle2); } 而(定时器间隔>5000); } 受保护的虚拟void OnActive(事件参数e) { //使用()语法触发事件 //一个测试变量,这样我们就可以对null进行可靠性测试, //如果没有订户。 EventHandler activeEventTest=ActiveEvent; if(activeEventTest!=null) { activeEventTest(这是新的EventArgs()); } 如果(计时器间隔

C# .WriteLine(idle2); } 而(定时器间隔>5000); } 受保护的虚拟void OnActive(事件参数e) { //使用()语法触发事件 //一个测试变量,这样我们就可以对null进行可靠性测试, //如果没有订户。 EventHandler activeEventTest=ActiveEvent; if(activeEventTest!=null) { activeEventTest(这是新的EventArgs()); } 如果(计时器间隔,c#,events,C#,Events,我建议不要将OnActive和OnInactive方法公开,否则会将太多的实现暴露给程序的其余部分。如果您希望从中继承类,那么请将它们设置为受保护的,否则我通常将它们设置为完全私有的,因为它们基本上是由类的其余部分调用的包装函数。我认为您需要对事件有更多的了解。让我通过一个示例代码来解释这一点 Class A{ public event OnInactive; public event OnActive; } 当classA中发生任何更改时,您希望更新ClassB中的内容。因此

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

我认为您需要对事件有更多的了解。让我通过一个示例代码来解释这一点

Class A{
   public event  OnInactive;
   public event  OnActive;
}
当classA中发生任何更改时,您希望更新ClassB中的内容。因此,您将在ClassB中实现类A的事件

链接将详细描述您


我的理解是,当你从同一个课堂上触发事件并在同一个课堂上听时,事件是没有用的。

我认为你需要的是对事件有更多的理解。让我通过一个示例代码来解释这一点

Class A{
   public event  OnInactive;
   public event  OnActive;
}
当classA中发生任何更改时,您希望更新ClassB中的内容。因此,您将在ClassB中实现类A的事件

链接将详细描述您



我的理解是,当你从同一个课堂触发事件并在同一个课堂上听时,事件是没有用的。

谢谢你的超级快速回答,然而,我已经尝试过这个解决方案,当我注释掉这些代码行时,事件似乎根本没有触发,根据我在web上的研究了解,我需要调用这个调用来触发事件,可能他指的是
base.onInactive()
@reeceottam-必须调用一次处理程序,否则,您将永远不会得到堆栈溢出。如何注册事件句柄?您的
OnInactive
OnActive
处理程序没有事件处理程序的预期签名。请澄清,我这样使用递归调用的理由是,我假设您需要它才能触发事件,这正是我试图实现的。它们不是事件处理程序,这些是将要调用的方法,以提高活动和非活动事件的响应速度,并为超级快速回答提供线索,然而,我已经尝试过这个解决方案,当我注释掉这些代码行时,事件似乎根本没有触发,根据我在web上的研究了解,我需要调用这个调用来触发事件,可能他指的是
base.onInactive()
@reeceottam-必须调用一次处理程序,否则,您将永远不会得到堆栈溢出。如何注册事件句柄?您的
OnInactive
OnActive
处理程序没有事件处理程序的预期签名。请澄清,我这样使用递归调用的理由是,我假设您需要它才能触发事件,这正是我试图实现的。它们不是事件处理程序,这些方法将被调用以引发活动和非活动事件。首先,删除所有断点并使用日志文件。您可以在下面的行中添加日志文件。您可以添加以下行来添加日志文件。AppendAllText(路径,“MessageValueOrVariableValue”)有点模糊,我只是想将此事件发送到fireNo,我正在尝试将此事件发送到fireNo,但目前似乎没有。否,我正在尝试将此事件发送到fireNo,在重要的时刻,它似乎没有这样做!我遗漏了受保护方法的“virtual”关键字。重要编辑!我省略了受保护方法的“virtual”关键字。
public event EventHandler InActive;
EventHandler inactiveEvent = this.InActive;
if(inactiveEvent != null)
{
    inactiveEvent(this, e);
}
class SpecialDerived : Base
{
    public override void Say()
    {
        Console.WriteLine("Called from Special Derived.");
        base.Say();
    }
}
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.
}
Class A{
   public event  OnInactive;
   public event  OnActive;
}