0

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

  • Run function every minute
  • Run MySQL command to get count
  • Store the count into a variable
  • Compare the result to old result every minute
  • Create an If statement if number is not same to previous number.

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?

LionBar
  • 1
  • 2
  • If the process is that simple you might want to just use `Thread.Sleep(60000)` to make the code wait 1min. – devcrp Jun 04 '20 at 09:27
  • Yes, `if (count != prev_count)` is the correct way to compare the old number to new number. – GSerg Jun 04 '20 at 09:27
  • @devcrp at the bottom of my code? – LionBar Jun 04 '20 at 09:28
  • yep, last line in side your `while` – devcrp Jun 04 '20 at 09:29
  • 2
    Pretty much everything else is wrong though. Start with understanding what `ExecuteNonQuery` does, and, consequently, what gets stored in `count`. – GSerg Jun 04 '20 at 09:29
  • @GSerg `ExecuteScalar()` should be the correct one? Because it returns me one row compared to the one I am using will be multiple rows etc.. – LionBar Jun 04 '20 at 09:31
  • 1
    Yes, it should be. Then think about who is calling `Method()` and is it correct that it never returns, as opposed to e.g. returning from it and having the thing that currently calls it call it every 60 seconds (including with the aforementioned `Thread.Sleep(60000)`). – GSerg Jun 04 '20 at 09:32
  • @GSerg You've lost me.. can you expand please? – LionBar Jun 04 '20 at 09:35
  • Where would you call this function? – Eduards Jun 04 '20 at 09:40
  • @LV98 when the dashboard screen loads. – LionBar Jun 04 '20 at 09:40
  • The idea is to notify the user is record has been inserted into MySQL – LionBar Jun 04 '20 at 10:47
  • Hello, i would recommend looking at the following post as well [link](https://stackoverflow.com/questions/30462079/run-async-method-regularly-with-specified-interval), it makes use of TPL-library in which you get extra benifits such as cancellation. – Jochem Van Hespen Jun 04 '20 at 10:55

1 Answers1

1

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();
    }
Artavazd
  • 132
  • 1
  • 6