Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to synchronize multiple background worker class in wpf?
    primarykey
    data
    text
    <p>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. </p> <pre><code> for (int i = 1; i &lt; 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); } } </code></pre> <p>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?</p> <p><strong>EDIT:</strong></p> <p>To make it clear I am updating with more detail.</p> <pre><code> &lt;my:DataGrid x:Name="theGrid" RowHeight="30" ItemsSource="{Binding Category}" AutoGenerateColumns="True" HeadersVisibility="All" Margin="235,96.5,84,65.5"&gt; &lt;my:DataGrid.RowDetailsTemplate&gt; &lt;DataTemplate&gt; &lt;Expander&gt; &lt;my:DataGrid Height="300" ItemsSource="{Binding Products}" AutoGenerateColumns="True" HeadersVisibility="Column"&gt; &lt;/my:DataGrid&gt; &lt;/Expander&gt; &lt;/DataTemplate&gt; &lt;/my:DataGrid.RowDetailsTemplate&gt; &lt;/my:DataGrid&gt; //List of objects List&lt;Category&gt; Categories = new List&lt;Category&gt;(); private void button1_Click(object sender, RoutedEventArgs e) { for (int i = 1; i &lt; 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&lt;Product&gt;(); for (long j = 1; j &lt; 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&lt;Product&gt; Products { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload