Displaying progress of processing collection

107 views Asked by At

I'm wondering what is a best practice to handle processing a collection of items and display the progress back to the UI.

The following code is roughly what I wrote to handle it, but it seems quite dirty:

Command = ReactiveCommand.CreateAsyncTask(_ => {
   return Task.Run(() => 
   {
       Parallel.ForEach(items, item =>
       {
           RunSlowProcess(item);

           Application.Current.Dispatcher.BeginInvoke(x =>
           {
               Progress += 1;
           }
       });
   }
}

Is there some major concept of ReactiveUI that I'm missing?

1

There are 1 answers

0
paulpdaniels On BEST ANSWER

It seems a bit confusing because of all the parallel patterns are mixed together here:

I would suggest something more like (untested):

Command = ReactiveCommand.CreateAsyncObservable(_ => {

  return items.ToObservable()
              .SelectMany(RunSlowProcess, 
                         (item, itemIndex, processed) => itemIndex);

})
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(i => Progress = i);