I was reading this article: http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/ So playing with the windows services I encountered a little problem due to my lack of knowledge. In this part of the code:
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
}
The this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
gives:
Error 1 No overload for 'timer1_Tick' matches delegate 'System.Timers.ElapsedEventHandler'
I wonder why since many people don't have a problem with that example?
An event is nothing else as a multicast delegate. And your method signature does not match the delegate signature, in your case the ElapsedEventHandler delegate.
You have to change your code:
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
}
You could also instead of:
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
Do this:
timer.Elapsed += this.timer1_Tick;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With