title: C# Updating UI in Windows Forms from background thread
body: <p><a href="http://www.codeproject.com/KB/threads/winformsthreading.aspx" rel="nofollow">http://www.codeproject.com/KB/threads/winformsthreading.aspx</a></p>
  
  <p>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)</p>
  
  <p>Here is my code. </p>
  
  <pre><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 &lt; '" + now + "'");
          expiredPoliciesNum = dt.Rows.Count;
  
          dt = sqliteQuery.selectFromDatabase("*", "WHERE ALPolicy = '1' AND ALExpiration &lt; '" + now + "'");
          expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;
  
          dt = sqliteQuery.selectFromDatabase("*", "WHERE WCPolicy = '1' AND WCExpiration &lt; '" + now + "'");
          expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;
  
          dt = sqliteQuery.selectFromDatabase("*", "WHERE ULPolicy = '1' AND ULExpiration &lt; '" + now + "'");
          expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;
  
  
          expiredPoliciesLabel.Text = expiredPoliciesNum + " Expired Policies       ";
          missingPoliciesLabel.Text = missingPoliciesNum + " Missing Policies       ";
      }
  </code></pre>
  
  <p>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.</p>