Это должно быть действительно просто, так как ваши окончательные данные зависят от результата всех четырех запросов.
Что вы можете сделать, это создать 4 асинхронных делегата, каждый из которых указывает на соответствующий веб-метод. Сделайте BeginInvoke на всех из них. А затем используйте WaitHandle, чтобы ждать всех. В вашем случае нет необходимости использовать обратные вызовы, поскольку вы не хотите продолжать работу во время обработки веб-методов, а скорее ждете, пока все веб-методы завершат выполнение.
Только после выполнения всех веб-методов будет выполняться код после оператора ожидания. Здесь вы можете объединить 4 результата.
Вот пример кода, который я разработал для вас:
class Program
{
delegate string DelegateCallWebMethod(string arg1, string arg2);
static void Main(string[] args)
{
// Create a delegate list to point to the 4 web methods
// If the web methods have different signatures you can put them in a common method and call web methods from within
// If that is not possible you can have an List of DelegateCallWebMethod
DelegateCallWebMethod del = new DelegateCallWebMethod(CallWebMethod);
// Create list of IAsyncResults and WaitHandles
List<IAsyncResult> results = new List<IAsyncResult>();
List<WaitHandle> waitHandles = new List<WaitHandle>();
// Call the web methods asynchronously and store the results and waithandles for future use
for (int counter = 0; counter < 4; )
{
IAsyncResult result = del.BeginInvoke("Method ", (++counter).ToString(), null, null);
results.Add(result);
waitHandles.Add(result.AsyncWaitHandle);
}
// Make sure that further processing is halted until all the web methods are executed
WaitHandle.WaitAll(waitHandles.ToArray());
// Get the web response
string webResponse = String.Empty;
foreach (IAsyncResult result in results)
{
DelegateCallWebMethod invokedDel = (result as AsyncResult).AsyncDelegate as DelegateCallWebMethod;
webResponse += invokedDel.EndInvoke(result);
}
}
// Web method or a class method that sends web requests
public static string CallWebMethod(string arg1, string arg2)
{
// Code that calls the web method and returns the result
return arg1 + " " + arg2 + " called\n";
}
}