c# - Update View in "Realtime" with data from ObservableCollection in MVVMLight -


i'm writing sort algorithm visualizer in c#/wpf using mvvmlight , fody. in viewmodel have observable collection this:

public observablecollection<int> numberscollection{ get; set; } 

now in command (method) change contents of collection (in case i'm doing bubble sort). this:

        imutablesortalgorithm sorter = servicelocator.current.getinstance<imutablesortalgorithm>();         sorter.muatblesort(this.numberscollection);          while (!sorter.finished())         {             sorter.nextstep();                                this.raisepropertychanged("numberscollection"); // not neccesary                thread.sleep(400);         } 

after call of sorter.nextstep() collection changed. after each step tried update view calling raisepropertychanged , sleeping 400ms.

so want view update after every change (step), view updated after method finished. (and don't need call raisepropertychanged...)

i use background worker convenience. first want solve problem without additional worker thread. since i'm in main gui thread, there's no need calling dispatcher.invoke, right? (i tried , didn't work anyway..).

does have clue how refresh ui after each step?

edit: sorter.nextstep() not insert or remove items list, swaps values. col[i] = col[j] ... know - sorting stuff. want changes in realtime on ui.

the collection in ui thread updating/sleeping , never processes property changed events.

the collection needs updated in separate thread. task.run, backgroundworker, async action etc.

that'll half way, resulting in error "this type of collectionview not support changes sourcecollection thread different dispatcher thread."

some googling found thread safe observable collection: where thread-safe collectionview?

decided randomize digits instead of sorting them , works me

    public mtobservablecollection<int> numberscollection     {         { return _item.numberscollection; }     }      public icommand randomizecommand     {                 {             if (_randomizecommand == null)                 _randomizecommand = new relaycommand(() =>                 {                                             task.run(()=>                     {                         (int = 0; < 10; i++)                         {                             _dataservice.randomize();                                                             thread.sleep(timespan.fromseconds(3));                         }                     });                                         });              return _randomizecommand;         }     } 

edit: approach (which learned from) insert delay/wait in code c#

leverage dispatcherhelper.runasync() await task.delay run in ui thread without blocking , can use regular observable collection instead of thread safe one.

    public  observablecollection<int> numberscollection     {         { return _item.numberscollection; }     }      public icommand randomizecommand     {                 {             if (_randomizecommand == null)                 _randomizecommand = new relaycommand(() =>                 {                     //task.run(() =>                     dispatcherhelper.runasync(async () =>                     {                         (var = 0; < 10; i++)                         {                             _dataservice.randomize();                             //thread.sleep(timespan.fromseconds(3));                             await task.delay(timespan.fromseconds(3));                         }                     });                 });              return _randomizecommand;         }     } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -