by Dominic Zukiewicz
17. June 2008 07:58
In the first part of this article, I discussed the option of calling multiple web services and waiting for them to all return.
It turns out you can do it quickly with a method call that already existed:
public List GetProducts( int[] productCodes )
{
ArrayList asyncStore = new ArrayList();
List<int, ProductWebService.Product> products = new List<int, ProductWebService.Product>();
ProductWebService.Products service = new ProductWebService.Products();
service.GetProductDataCompleted += new GetProductDataCompletedEventHandler(GetProductData_Completed);
GetProductDataDelegate asyncCall = new GetProductDataDelegate(service.GetProductData);
foreach(int productCode in productCodes)
{
IAsyncResult result = asyncCall.BeginInvoke(productCode,null,productCode);
asyncStore.Add(result.AsyncWaitHandle);
}
WaitHandle[] waitHandles = (WaitHandle[])asyncStore.ToArray(typeof(WaitHandle[]));
WaitHandle.WaitAll ( waitHandles );
//Once here, we can guarantee all of the web services have fired!
}
protected void GetProductData_Completed(object sender, GetProductDataEventArgs e)
{
//Single web service fired
}
Therefore, you can use WaitAll() for your web services!