Multithreaded Windows Phone Application

I want the data retrieval to load not in the ui thread, but in the background thread. I tried some examples from Google, but it still does not work.

ThreadPool.QueueUserWorkItem((o) => { IList<Asana> asanasRepo = null; var asanasRepository = this.GetService<IAsanasRepository>(); asanasRepo = asanasRepository.GetAllAsanas(); Asanas = asanasRepo.Select(x => new AsanasListItemViewModel { AsanaId = x.AsanaId, AsanaLevel = InfrastructureHelper.GetLevel(x.AsanaLevel), CoverImagePath = string.Format("/Content/Images/{0}", x.CoverImageFileName), UsualAsanaTitle = x.UsualTitle, YogaAsanaTitle = x.YogaTitle }).ToObservableCollection(); asanasDispatcher.BeginInvoke(() => { AsanasItems.Clear(); AsanasItems = (from asana in Asanas group asana by asana.AsanaLevel into c orderby c.Key select new Group<AsanasListItemViewModel>(c.Key, c) ).ToObservableCollection(); }); }); 

The main idea is to load data from the database, not into the ui stream, but in the background, when the data is downloaded, output it to ui. I tried something like the code above, but it does not work. can you help me with this? Thanks!

+4
source share
1 answer

Try to initialize AsanasItems an empty ObervableCollection<..> in the constructor, and in the code you showed do not assign a new collection to it, just use AsanasItems.Add(..) to add elements one at a time.

If this does not work, you need to provide a minimal working sample for anyone who can help you.

+3
source

Source: https://habr.com/ru/post/1492581/


All Articles