Note that there are some explanatory texts on larger screens.

plurals
  1. POView is not updated when on a separate thread
    primarykey
    data
    text
    <p>I am trying to load some data in a separate thread, then add the loaded data to an ObservableCollection and update the view through ba binding.</p> <p>First, I was doing the following:</p> <pre><code>public OverviewViewModel() { Thread thread = new Thread(new ThreadStart(delegate { TheTVDB theTvdb = new TheTVDB(); foreach (TVSeries tvSeries in theTvdb.SearchSeries("Dexter")) { this.Overview.Add(tvSeries); } })); thread.SetApartmentState(ApartmentState.STA); thread.Start(); } </code></pre> <p>This gave the following error:</p> <blockquote> <p>This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.</p> </blockquote> <p>So I read on this forum that I should use the Dispatcher, so I put <code>this.Overview.Add(tvSeries)</code> into a call to the Dispatcher.</p> <pre><code>Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate { this.Overview.Add(tvSeries); }, DispatcherPriority.Normal); </code></pre> <p>Now, it doesn't crash anymore but the view is not updated. Nothing happens, the view is just empty. I have tested the functionality by running it on the main thread. Then the view is updated correctly.</p> <p>Does anyone know why the view is not updated and how I can fix this?</p> <p><strong>UPDATE</strong></p> <p>The below approach seems to work and it seems to do everything asynchronously. Can anyone confirm that this is the right approach for doing things asyncronously?</p> <pre><code>Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate { TheTVDB theTvdb = new TheTVDB(); foreach (TVSeries tvSeries in theTvdb.SearchSeries("Dexter")) { this.Overview.Add(tvSeries); } }), DispatcherPriority.Background); </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