I have created a Table in SQL and it has only one Column, its value may change over time.
If my Column's value is greater than 10, I want to show a Label, if it is less than 10 the Label text should change its text.
I have tried this using BackgroundWorker, but it is not working as expected
The BackgroundWorker should never stop querying the database. If the value changes in the SQL table, then the value should change in the Label.
The code in the BackgroundWorker should always check the new value.
private bool sqlData()
{
try
{
SqlConnection con = new SqlConnection(
@"Server=NTN\TEST; Database=WinApp;User ID=sa; pwd=Rh#yt$33q");
SqlCommand cmd = new SqlCommand("select * from Timer_test", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
value = (int)rdr["value"];
}
}
con.Close();
if (value > 10)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (sqlData())
{
timer1.Start();
timer1.Enabled = true;
Thread.Sleep(0100);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
inpo.Text = "value less than 10";
backgroundWorker1.RunWorkerAsync();
}
private void Home_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}