-1

I am making a timer to run a specific piece of code every 24 hours, I am using a timer to do this, but I am getting the following error when I try to run my code.

Error 1 'System.Windows.Forms.Timer' does not contain a definition for 'Elapsed' and no extension method 'Elapsed' accepting a first argument of type 'System.Windows.Forms.Timer' could be found (are you missing a using directive or an assembly reference?)

Do i need to use System.Timer instead? As when I do that, it doesn't work either.

Here is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Timers;

private void startBtn_Click_1(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
        myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
        myTimer.Interval = 1000; // 1000 ms is one second
        myTimer.Start();
    }


public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
    {
        backUp();
    }
Nezz
  • 41
  • 2
  • 11

2 Answers2

0

Sorry for wasting anyones time.

I did this... and now it works!

System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 1000; // 1000 ms is one second
myTimer.Start();
Nezz
  • 41
  • 2
  • 11
  • I tried this but not working. The type or namespace name 'ElapsedEventHandler' could not be found (are you missing a using directive or an assembly reference?) – Hilal Al-Rajhi Mar 18 '20 at 14:26
0

The event you're looking for is called Tick in the System.Windows.Forms.Timer type:

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += myTimer_Tick;

void myTimer_Tick(object sender, EventArgs e)
{
    Backup();
}
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76