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 8219257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:05:30+00:00 2026-06-07T13:05:30+00:00

I’m trying to code part of my application that runs a BackgroundWorker process that

  • 0

I’m trying to code part of my application that runs a BackgroundWorker process that performs a time-consuming operation. In the main thread, a timer updates a progress bar (this is a continuation of this question). However, this code display no MessageBoxes. Setting a breakpoint on the foreach (String word in this.words) line in the SearchButton_Click event handler reveals that this.words has no values, i.e. this.words.Count() == 0.

public partial class Form1 : Form
{
    System.Windows.Forms.Timer searchProgressTimer;
    List<String> words;

    public Form1()
    {
        InitializeComponent();

        words = new List<String>(3);
    }

    private void SearchDatabase_Click(object sender, EventArgs e)
    {
        this.searchProgressTimer.Start();
        SearchBackgroundWorker.RunWorkerAsync();

        foreach (String word in this.words) // BREAKPOINT HERE
            MessageBox.Show(word);
    }

    private void SearchBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Time-consuming operation
        String filename = @"http://www.bankofengland.co.uk/publications/Documents/quarterlybulletin/qb0704.pdf";
        WebClient webClient = new WebClient();
        webClient.DownloadFileAsync(new Uri(filename), @"file.pdf");
        List<String> word_result = new List<String> { "word1", "word2", "word3" };
        e.Result = word_result; // e.result is an Object, and word_result is a List.
    }

    private void SearchBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.searchProgressTimer.Stop();
        this.searchProgressBar.Value = 0;
        this.words = (List<String>)e.Result;
    }   
}

My guess as to why this occurs is because the BackgroundWorker thread isn’t finished with its operation before the main UI thread moves on to the foreach loop. I think I understand that part. However, since I want to perform the time-consuming operation in a background thread so the progress bar can update its value as said operation runs, then use the result of the BackgroundWorker immediately after its finished, how would I do this?

Please edit my title if it doesn’t get the point across as well. I wasn’t sure how to phrase this.

  • 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-07T13:05:32+00:00Added an answer on June 7, 2026 at 1:05 pm

    Do whatever you want to do in that RunWorkerCompleted event:

    private void SearchBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.searchProgressTimer.Stop();
        this.searchProgressBar.Value = 0;
        this.words = (List<String>)e.Result;
    
        foreach (String word in this.words) // BREAKPOINT HERE
            MessageBox.Show(word);
    }
    

    Since you are getting this information from the background worker, the only way you know you have your list is when the worker completes.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
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.