Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload for matches delegate 'System.Timers.ElapsedEventHandler'

Tags:

c#

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?

like image 731
CuriousMind Avatar asked Nov 28 '25 10:11

CuriousMind


1 Answers

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;
like image 183
CodeTherapist Avatar answered Nov 30 '25 00:11

CodeTherapist