Reputation: 2301
http://www.codeproject.com/KB/threads/winformsthreading.aspx
I'm attempting to use the above, and while it works in some sense (it doesn't lock up my application), it isn't updating the labels on the UI unfortunately. Am I going wrong somewhere? I have two labels that are on the MainForm, expiredPoliciesLabel and missingPoliciesLabel. To update them, I have to set expiredPoliciesNum and missingPoliciesNum by doing a series of database queries, as you can see. I need the labels to update automatically every minute or so. (I know right now I have it set at 1 second, it's only to see if the code is working)
Here is my code.
public delegate void updatePolicyLabelsDelegate();
public partial class MainForm: Form
{
SQLiteQuery sqliteQuery = new SQLiteQuery(Properties.Settings.Default.DatabasePath);
int expiredPoliciesNum = 0;
int missingPoliciesNum = 0;
Thread minimizeThread;
public MainForm()
{
this.Resize += new EventHandler(MainForm_Resize);
this.IsMdiContainer = true;
InitializeComponent();
this.ShowInTaskbar = false;
keyValidation();
Thread bottomLabelsThread = new Thread(new ThreadStart(updateLabels));
bottomLabelsThread.IsBackground = true;
}
public void updateLabels()
{
while (true)
{
Invoke(new updatePolicyLabelsDelegate(updatePolicyLabels));
Thread.Sleep(1000);
}
}
private void updatePolicyLabels()
{
DataTable dt = sqliteQuery.selectFromDatabase("*", "WHERE GLOPolicy != '1'");
missingPoliciesNum = dt.Rows.Count;
dt = sqliteQuery.selectFromDatabase("*", "WHERE ALPolicy != '1'");
missingPoliciesNum = missingPoliciesNum + dt.Rows.Count;
dt = sqliteQuery.selectFromDatabase("*", "WHERE WCPolicy != '1'");
missingPoliciesNum = missingPoliciesNum + dt.Rows.Count;
dt = sqliteQuery.selectFromDatabase("*", "WHERE ULPolicy != '1'");
missingPoliciesNum = missingPoliciesNum + dt.Rows.Count;
String now = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
dt = sqliteQuery.selectFromDatabase("*", "WHERE GLOPolicy = '1' AND GLOExpiration < '" + now + "'");
expiredPoliciesNum = dt.Rows.Count;
dt = sqliteQuery.selectFromDatabase("*", "WHERE ALPolicy = '1' AND ALExpiration < '" + now + "'");
expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;
dt = sqliteQuery.selectFromDatabase("*", "WHERE WCPolicy = '1' AND WCExpiration < '" + now + "'");
expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;
dt = sqliteQuery.selectFromDatabase("*", "WHERE ULPolicy = '1' AND ULExpiration < '" + now + "'");
expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;
expiredPoliciesLabel.Text = expiredPoliciesNum + " Expired Policies ";
missingPoliciesLabel.Text = missingPoliciesNum + " Missing Policies ";
}
Thanks for any help, I'm new to threading, and could use some expertise, and believe it or not, I've searched everywhere trying to figure out where I'm going wrong to no avail.
Upvotes: 2
Views: 6201
Reputation:
You created the thread object, but you don't start it, as your code seems to show:
bottomLabelsThread.Start();
It is not started automatically.
Upvotes: 8