c# - 'onTimer' 没有重载匹配委托(delegate) 'ElapsedEventHandler'

标签 c# delegates system.timers.timer

通常,当人们收到“'onTimer' 没有重载匹配委托(delegate) 'ElapsedEventHandler'”错误时,这是​​由于没有在每次计时器结束时运行的 void 中包含 ElapsedEventArgs 参数。此外,当我将代码复制并粘贴到解决方案之外的文件中时,错误似乎消失了,所以我很困惑。有人知道更简单的方法来解决这个问题吗?

    static void setTimer()
    {
        yearIncreaser.Elapsed += onTimer;
        yearIncreaser.Enabled = true;
        yearIncreaser.AutoReset = true;
    }

    static void onTimer(Object source, ElapsedEventArgs e)
    {
        currentYear++;
    }

编辑:是的,调试屏幕产生的官方错误消息是:错误 CS0123:'onTimer' 没有重载匹配委托(delegate) 'ElapsedEventHandler'

EDIT2:解决方案正在改变 yearIncreaser.Elapsed += onTimer;进入 lambda:yearIncreaser.Elapsed += (sender, args) => { }

最佳答案

也许其他人会需要它。

我只是将对象更改为对象:

    static void setTimer()
    {
        yearIncreaser.Elapsed += onTimer;
        yearIncreaser.Enabled = true;
        yearIncreaser.AutoReset = true;
    }

    static void onTimer(object source, ElapsedEventArgs e)
    {
        currentYear++;
    }

关于c# - 'onTimer' 没有重载匹配委托(delegate) 'ElapsedEventHandler',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60695943/

相关文章:

c# - 为 asp.net MVC 静态资源设置合成路由

c# - 如何在 C# 中从 json 反序列化/解析字符串中获取值和键?

objective-c - 在自定义委托(delegate) block UI 中执行工作——为什么?

c# - System.Timers.Timer 如何获取 Elapse 之前的剩余时间

c# - 如何找出 ADUser 的密码到期日期或密码到期前剩余的天数?

C# 使用 MySql 数据库中的 true/false 来继续或显示错误

c# - 传递给委托(delegate)时作为引用类型处理的整数

c# - 使用委托(delegate)记录类 (NullReferenceException)

server - Blazor 服务器端计时器正在运行

c# 使用 System.Timers 时取消任务