title: Thread contained inside class
body: <p>I'm writing a simple Windows forms application to get me into the swing of things with Threads. So far what I have is working, but what I would like to do is contain it all in a seperate class rather than directly in my forms code. </p>
  
  <p>I have a background thread that starts and retrieves data from a database. I then display that data in to a listbox.</p>
  
  <pre><code>private delegate void UpdateListValues(List&lt;ListBoxItem&gt; itemList);
  
  private void form_main_Shown(object sender, EventArgs e)
  {            
      // Set the loading text.
      list_selection.Items.Add(ListHelpers.LoadingItem());
  
      // Start the data access on a seperate thread.
      Thread worker = new Thread(GetInvoicingData);
      worker.IsBackground = true;
      worker.Start();
  }
  
  private void GetInvoicingData()
  {
      // Query database
      List&lt;ListBoxItem&gt; values = DAC.GetInvoicingAccounts();
  
      // Display results
      BeginInvoke(new UpdateListValues(DisplayList), new object[] { values });
  }
  
  private void DisplayList(List&lt;ListBoxItem&gt; itemList)
  {
      // Display each result
      list_selection.Items.Clear();
      for (int i = 0; i &lt; itemList.Count; i++)
      {
          list_selection.Items.Add(itemList[i]);
      }
  }
  </code></pre>
  
  <p>The problem is that in the DisplayList method, I won't be able to access the list box (list_selection) because it's part of the form class. Does anyone have any suggestions on how I can do this. </p>
  
  <p>Also, I'm new to threading so feel free to tell me I'm doing it absolutely wrong. I just used the example from <a href="http://www.codeproject.com/Articles/23517/How-to-Properly-Handle-Cross-thread-Events-and-Upd" rel="nofollow">http://www.codeproject.com/Articles/23517/How-to-Properly-Handle-Cross-thread-Events-and-Upd</a> to get me to where I am now.</p>
  
  <p>Thanks</p>