gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:27:45 26 4
gpt4 key购买 nike

我正在阅读这篇文章:http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/因此,由于缺乏知识,我在使用 Windows 服务时遇到了一个小问题。在这部分代码中:

 protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 10800;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
}

private void timer1_Tick()
{
//some code here
}

protected override void OnStop()
{
timer1.Enabled = false;
//some code here

}

this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);给出:

Error 1 No overload for 'timer1_Tick' matches delegate 'System.Timers.ElapsedEventHandler'

我想知道为什么很多人对这个例子没有异议?

最佳答案

事件就是多播委托(delegate)。并且您的方法签名与委托(delegate)签名不匹配,在您的情况下是 ElapsedEventHandler 委托(delegate)。

你必须改变你的代码:

 protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 10800;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
}

private void timer1_Tick(object sender, ElapsedEventArgs elapsedEventArg)
{
//some code here
}

protected override void OnStop()
{
timer1.Enabled = false;
//some code here

}

你也可以代替:

this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);

这样做:

timer.Elapsed += this.timer1_Tick;

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com