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

  • SEARCH
  • Home
  • 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 4040308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:44:11+00:00 2026-05-20T12:44:11+00:00

I just wrote a simple app to learn multi-threading, and I’m missing something. I

  • 0

I just wrote a simple app to learn multi-threading, and I’m missing something. I start a new thread that performs a relatively lengthy database operation (checking SharePoint permissions on users for a particular site), often up to fifteen seconds. This is how I am creating the thread (removing some extraneous code for simplicity):

private void btnSelectSite_Click(object sender, EventArgs e)
{
    strSiteURL = txtSiteURL.Text;

    tmrProgressTimer.Interval = 1000;
    tmrProgressTimer.Enabled = true;

    ThreadStart starter = delegate { LoadUsers(strSiteURL); };
    Thread t = new Thread(starter);
    t.Start();
    t.Join();

    cboUsers.Items.Clear();
    cboUsers.Items.AddRange(list.ToArray());

    tmrProgressTimer.Enabled = false;
}

I am using a delegate to fire off LoadUsers in its own thread, since LoadUsers requires a string. It populates a generic list (“list” in the code) and I later use that to populate a combobox. My understanding is that while this thread is processing, my UI shouldn’t lock up, being as it is on its own thread; however, that has not been the case. None of the UI refreshes until after the thread finishes, and the app is locked up during the thread processing — the timer never even fires, although it should be ticking every second and the database operation is taking up to fifteen. Could someone tell me what I’m doing wrong?

  • 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-05-20T12:44:12+00:00Added an answer on May 20, 2026 at 12:44 pm

    As Yuriy answered, your UI is blocked, waiting for the thread t to finish its work thanks to the call to :

    t.Join();
    

    Note: the edited solution provided by Yuriy does not work either, since the combo gets updated from the worker thread, not the UI, as Ego discovered (cross-thread accesses are not allowed).

    However, if you don’t do that, your code won’t work either, since you seem to be expecting that the list was already populated by the worker thread. This most certainly won’t be the case.

    In order to get back the results to your combo box, you’ll have (in pre-C# 5 days, at least) to do some additional processing when your LoadUsers method returns. Here is a suggestion of how you could implement it:

    private void ...
    {
        strSiteURL = ...
    
        System.Action after =
            delegate
            {
                cboUsers.Items.Clear();
                cboUsers.Items.AddRange(list.ToArray());
            };
    
        ThreadStart starter =
            delegate
            {
                LoadUsers(strSiteURL);
                this.Invoke(after);
            };
    
        Thread t = new Thread(starter);
        t.Start();
    }
    

    When your thread returns from LoadUsers you’ll have to update the combo box. But you cannot do so in a worker thread: it must be done on the original UI thread. To do so, you’ll have to call the Invoke method provided by your Form (see MSDN to learn more about invoking), passing it a delegate. The after delegate will be run on the UI thread and all will be well.

    I’d also add some code to disable the button, so that the user does not start more than one thread. And when the thread invokes after, you could finish by reenabling the button.

    And beware of exceptions 🙂

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

Sidebar

Related Questions

No related questions found

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.