次の方法で共有


Don’t ForkJoin your Events

I’ve been using Reactive Extensions for .NET to attempt to wait until multiple async calls to load data are completed in order to perform some actions on all the data at once.

Dev Tidbit of the Day

Do not use ForkJoin with Event based observables.

  1. If you create an IObservable from a .NET event, the Observable sequence will never complete. (That makes sense since the event can be raised multiple unknown times)
  2. ForkJoin waits for the all input Observables to complete.

 

ergo, if you create some Observables using FromEvent and then attempt to join them with ForkJoin like this

 1 var result = Observable.ForkJoin(oFromEvent1, oFromEvent2);
2 result.Subscribe(eventArgs =>
3     {
4        // do stuff with eventArgs[0] & eventArgs[1] – never gets called :-(
5     }

They will never get joined.

Use Zip instead.

  1     var result = Observable.Zip(
 2                   oFromEvent1, 
 3                   oFromEvent2, 
 4                   (event1, event2) => new { event1, event2}
 5                   );
 6 
 7     result.Subscribe(eventArgs =>
 8         {
 9             // do stuff with eventArgs.event1 & eventArgs.event2
10         });