I’d like to call two webservices simultaneously and process the responses when both are done. I am calling the webservices using Rx’s Observable.FromAsyncPattern method. What is the correct method to simultaneously subscribe to multiple IObservables?
I’ve tried using Zip, but it does not appear to start both simultaneously, only starting the second after the first result is received.
EDIT:
Here’s a demonstration of Zip or some of the other solutions proposed not solving my problem —
class Program
{
static void Main(string[] args)
{
var observable1 = Observable.Create<int>(i =>
{
Console.WriteLine("starting 1");
System.Threading.Thread.Sleep(2000);
Console.WriteLine("done sleeping 1");
i.OnNext(1);
i.OnCompleted();
return () => { };
});
var observable2 = Observable.Create<int>(i =>
{
Console.WriteLine("starting 2");
System.Threading.Thread.Sleep(4000);
Console.WriteLine("done sleeping 2");
i.OnNext(1);
i.OnCompleted();
return () => { };
});
var m = observable1.Zip(observable2, (a, b) => new { a, b });
var n = Observable.Merge(Scheduler.ThreadPool,
observable1, observable2);
var o = Observable.When(observable1.And(observable2).Then((a, b) => new { a, b }));
m.Subscribe(
(i) => Console.WriteLine(i),
() => Console.WriteLine("finished"));
Console.Read();
}
}
Results:
starting 1
done sleeping 1
starting 2
done sleeping 2
{ a = 1, b = 1 }
finished
Desired Results:
starting 1
starting 2
done sleeping 1
done sleeping 2
{ a = 1, b = 1 }
finished
Using the
Zipextension method is the simple answer here.If you have a couple of typical async calls (assuming single parameter in):
Then you can call both and handle there return values once both are completed like so: