[Solved] repeat function every second


1- Use System.Timers.Timer

2- Set Interval to 1 Second

3- Handle the Elapsed Event

4- Make you method thread safe by using lock, so that no 2 timers will insert to the database at the same time.

var mytimer = new System.Timers.Timer(1000);
mytimer.Elapsed += Mytimer_Elapsed;
mytimer.Start();

private static object lockobject = new object();
private static void Mytimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    lock(lockobject)
    {
        cn.Open();

        try
        {
            MySqlCommand cmd = new MySqlCommand("insert into supervision values("...........')", cn);
            /
            cmd.ExecuteNonQuery();
        }
        catch (MySqlException ex)
        {
            if (ex.Number == 2627)
            {
                MySqlCommand cmd2 = new MySqlCommand("insert into supervision values(" +.......')", cn);


                cmd2.ExecuteNonQuery();
            }
        }
        cn.Close();
        MessageBox.Show("database added");
    }
}

solved repeat function every second