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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:49:48+00:00 2026-05-28T02:49:48+00:00

I have implemented background worker class something similar to below sample and I want

  • 0

I have implemented background worker class something similar to below sample and I want to update my UI each time the background worker is completed.

          for (int i = 1; i < 10; i++)
            {
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
                worker.RunWorkerAsync(i);

                while (worker.IsBusy == true)
                {
                   Thread.Sleep(100);
                }
            }

the Worker_DoWork return datarow and Worker_RunWorkerCompleted is adding returned result to data grid. but the function never reach in Worker_RunWorkerCompleted in a proper order. How could I can solve this?

EDIT:

To make it clear I am updating with more detail.

 <my:DataGrid x:Name="theGrid" RowHeight="30" ItemsSource="{Binding Category}" AutoGenerateColumns="True" HeadersVisibility="All" Margin="235,96.5,84,65.5">
            <my:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <Expander>
                        <my:DataGrid Height="300" ItemsSource="{Binding Products}" AutoGenerateColumns="True" HeadersVisibility="Column"> </my:DataGrid>
                    </Expander>
                </DataTemplate>
            </my:DataGrid.RowDetailsTemplate>
        </my:DataGrid>


    //List of objects
    List<Category> Categories = new List<Category>();

    private void button1_Click(object sender, RoutedEventArgs e)
    {      
        for (int i = 1; i < 10; i++)
        {
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
            worker.RunWorkerAsync(i);

            while (worker.IsBusy == true)
            {
                Thread.Sleep(100);
            }
        }
    }

    void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        long i = Convert.ToInt64(e.Argument);
        Category cat = new Category { CategoryID = i, Name = "Category" + i };
        cat.Products = new List<Product>();

        for (long j = 1; j < 10; j++)
        {
            Product p = new Product { ProductID = (i * j), Name = "Product " + (i * j).ToString() };
            cat.Products.Add(p);
        }

        e.Result = cat;
    }

    void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Category cat = ((Category)e.Result);
        Categories.Add(cat);
        theGrid.ItemsSource = Categories;
    }
}

public class Product
{
    public long ProductID { get; set; }
    public string Name { get; set; }
}

public class Category
{
    public long CategoryID { get; set; }
    public string Name { get; set; }
    public List<Product> Products { get; set; }
}
  • 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-28T02:49:49+00:00Added an answer on May 28, 2026 at 2:49 am

    You’re still blocking the UI thread – it won’t get to process events until all your BackgroundWorkers have completed. That goes against the whole point of BackgroundWorker. You should start them, and just let them finish. If you need to make some other change when they’ve finished, you should do that in the handler for RunWorkerCompleted – possibly keeping count of how many have finished (if you start them in parallel) or starting a new one if you want to run them in series, until you’ve run all the ones you want to run.

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

Sidebar

Related Questions

I have a Windows Application. We have implemented AutoSave functionality as background process. Sample
I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented VirtualPathProvider class so I can keep all my views in the
I have a GUI class and a database class. I do something like: Dim
I have a background thread. If the background thread is busy, I want to
I have implemented an HTTPOperations class in Android that can successfully produce an HTTP
Ok, I recently implemented a background worker to perform saving and loading of data.
Backgroup: We are looking at SAS BI Dashboard. We have currently implemented almost all
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a simple file upload-download mechanism. When a user clicks a file

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.