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; }
}
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 forRunWorkerCompleted– 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.