Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:44:10+00:00 2026-06-06T08:44:10+00:00

I’m writing a simple Windows forms application to get me into the swing of

  • 0

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.

I have a background thread that starts and retrieves data from a database. I then display that data in to a listbox.

private delegate void UpdateListValues(List<ListBoxItem> 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<ListBoxItem> values = DAC.GetInvoicingAccounts();

    // Display results
    BeginInvoke(new UpdateListValues(DisplayList), new object[] { values });
}

private void DisplayList(List<ListBoxItem> itemList)
{
    // Display each result
    list_selection.Items.Clear();
    for (int i = 0; i < itemList.Count; i++)
    {
        list_selection.Items.Add(itemList[i]);
    }
}

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.

Also, I’m new to threading so feel free to tell me I’m doing it absolutely wrong. I just used the example from http://www.codeproject.com/Articles/23517/How-to-Properly-Handle-Cross-thread-Events-and-Upd to get me to where I am now.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:44:14+00:00Added an answer on June 6, 2026 at 8:44 am

    How about something like this:

    // Added the form's class declaration to highlight separation of thread code into a separate class, but may not be exactly the same as yours depending on naming
    public class Form1 : Form
    {
        private readonly DataRetriever _dataRetriever;
    
        private void form_main_Shown(object sender, EventArgs e)
        {            
            // Set the loading text.
            list_selection.Items.Add(ListHelpers.LoadingItem());
    
            // Create the DataRetriever, and provide it with a delegate to DisplayList for returning data
            _dataRetriever = new DataRetriever(DisplayList);
            // Start retrieving data on a separate thread...
            _dataRetriever.GetData();
        }
    
        private void DisplayList(List<ListBoxItem> itemList)
        {
            if (InvokeRequired)
            {
                // Ensure the update occurs on the UI thread
                Invoke((Action)(() => DisplayList(itemList)));
                return;
            }
            // Display each result
            list_selection.Items.Clear();
            foreach (var item in itemList)
            {
                list_selection.Items.Add(item);
            }
        }
    }
    
    // Separate class to hold thread code
    public class DataRetriever
    {
        public delegate void UpdateCallbackDelegate(List<ListBoxItem> itemList);
    
        private readonly UpdateCallbackDelegate _updateCallback;
    
        public DataRetriever(UpdateCallbackDelegate updateCallback)
        {
            _updateCallback = updateCallback;
        }
    
        public void GetData()
        {
            var thread = new Thread(GetInvoicingData)
            {
                IsBackground = true
            };
            thread.Start();
        }
    
        private void GetInvoicingData()
        {
            // Not sure whether "DAC" is a static class, if it needs to be constructed
            // in the DataRetriever's constructor, or passed to it as a parameter
            _updateCallback(DAC.GetInvoicingAccounts());
        }
    }
    

    As you can see, all the thread code is now in a separate class DataRetriever, and a delegate provided when constructing it to enable the retrieved data to be passed back to the form once the retrieval is complete. The method that handles the callback ensures that the call is marshalled to the UI thread to prevent cross-thread exceptions.

    I would like to point out that this is not presented as the “best” way to do this, but merely as an answer to the question (how to separating threading code into a separate class). As others have mentioned, there are already mechanisms in place to do this sort of thing (e.g. BackgroundWorker). Some complexity has been omitted for clarity. For example, in the implementation presented here, if you were to call GetData() multiple times (with each call occurring before the previous ones have returned their data), you would have multiple queries occurring simultaneously, and as they are running asynchronously, may return their data in an arbitrary order. This may or may not be an issue in your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
Thanks in advance for your help. I have a need within an application to
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.