LionBar
LionBar

Reputation: 1

How to run a function every minute comparing old number to new number?

I'd like to run a function every minute to achieve the following:

Here is what I have:

 private void Method()
{
    int count = int.MinValue;
    int prev_count = int.MinValue;


    while (true)
    {

        //Get count from MySQL table
        using (var conn = new MySqlConnection(ConnectionString.ConnString))
        {
            conn.Open();
            using (var cmd = new MySqlCommand("select count(*) from table;", conn))
            {
                count = (int)cmd.ExecuteNonQuery();
            }
        }


        if (count != prev_count)
        {

            prev_count = count;
        }

    }
}

My question is - is the correct way of coding this to compare the old number to new number? and also, how can I make this function run every minute?

Upvotes: 0

Views: 133

Answers (1)

Artavazd
Artavazd

Reputation: 132

Here how to call an event hendeler to track each given time. Hope this will help you.

private static System.Timers.Timer timer;

    private static void YourFunction(object sender, EventArgs e)
    {
        timer.Stop();
        Console.WriteLine("Hello World!");
        timer.Start();
    }

    static void Main(string[] args)
    {
        timer = new System.Timers.Timer();
        timer.Interval = 5000; // time to configure
        timer.Elapsed += new System.Timers.ElapsedEventHandler(YourFunction);
        timer.Enabled = true;
        timer.Start();
        Console.ReadKey();
    }

Upvotes: 1

Related Questions