Goal:
I am aiming to notify the user then a new record is detected in MySQL table.
What I am using:
I am using Windows form
and MySQL
.
Also, within the code I am using "SELECT COUNT(*)"
and store it into a variable to check the previous count and the new count.
What I currently have coded:
public Dashboard()
{
InitializeComponent();
//Timer to run function every 10 seconds
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 10000;
timer.Elapsed += Notification;
timer.Start();
}
//Track changes in MySQL table - and if new records appear then notify the user
private void Notification(object sender, System.Timers.ElapsedEventArgs e)
{
//MySQL connection
using(var conn = new MySqlConnection(ConnectionString.ConnString))
{
conn.Open();
//Create command
using(var cmd = new MySqlCommand("SELECT COUNT(*) from tester", conn))
{
var dr = cmd.ExecuteReader();
while (dr.Read())
{
//Current Number
int count = dr.GetInt32(0);
//Previous number
int prev_numb = int.MinValue;
while (true)
{
//If count is not same as previous number
if(count != prev_numb)
{
//Notify user
MessageBox.Show("New Record!");
//Set prev_numb to new numb
prev_numb = count;
}
}
}
}
}
}
Problem:
The first part of the code works fine (Runs the function every 10 seconds).
But the messagbox keeps prompting every 10 seconds, even if the count
has not changed... why is this happening? And what needs amending within the code to work as desired?
Resources:
Repeat function every n minutes https://stackoverflow.com/a/34204679/12485722