title: Background Worker to populate combobox in windows form application
body: <p>I want to implement background worker in my windows application. 
  Currently I am using button event handler to load the combo box with data. As the query hangs the user interface, i would like to implement background worker as that the query runs in different thread. I have never used this background worker in any of my application. I did some research on this and still unable to implement this. Any help or advice will be greatly appreciated.</p>
  
  <p>This is how my button event handler looks like</p>
  
  <pre><code>private void button6_Click(object sender, EventArgs e)
          {
              if (comboBox1.SelectedItem.ToString() == "All")
              {
  
                 findAllUser();
  
              }
  
              else
              {
                  //Do Something!!!
  
              }
          }     
  </code></pre>
  
  <p>findAllUser() will fetch all the user from active directory which normally takes time and makes the UI unresponsive. Code for findAllUser() looks like this.</p>
  
  <pre><code>public void findAllUser()
      {
          System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://DC=xyz, DC=com");
          System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
          mySearcher.Filter = "(&amp;(objectClass=user))";
  
          foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
          {
              try
              {
                  System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();
                  comboBox2.Items.Add(de.Properties["GivenName"].Value.ToString() + " " + de.Properties["sn"].Value.ToString() + " " + "[" + de.Properties["sAMAccountName"].Value.ToString() + "]");
              }
  
              catch (Exception)
              {
                  // MessageBox.Show(e.ToString());
              }
          }
  
      }
  </code></pre>
  
  <p>Below is how the background worker looks now..all empty</p>
  
  <pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
  
  
          }
  
          private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
  
  
          }
  
          private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
  
          }
  </code></pre>
  
  <p>Any advice how can i implement the code above so that the background worker will populate the combobox2 with the active directory user list.</p>
  
  <p><img src="https://i.stack.imgur.com/ZDdIs.png" alt="enter image description here"></p>