0

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?

Regent
  • 5,142
  • 3
  • 21
  • 35
stehlikio
  • 68
  • 5

1 Answers1

0

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);
paulpdaniels
  • 18,395
  • 2
  • 51
  • 55