Execute multiple task and await all have completed

In this example I show how to do multiple call of a web service in async mode.
serviceClient my own proxy class to contact the web service.
This is the signature:

Task<List<StoreHours>> GetBusinessHours(int storeId);
Task<List<Flyer>> GetFlyerByStoreId(int storeId);

This solution call the service and wait it complete the first request, than i contact the service again and I wait the response

ListFlyer = await serviceClient.GetFlyerByStoreId(SelectedStore.id);
ListStoreHours = await serviceClient.GetBusinessHours(SelectedStore.id);

ok if the waiting time to contact the service is about 200ms to complete the 2 requests I probably wait 400ms
Too Bad!

Ok but if If I call the service at the same time to complete all requests I probably wait 200ms, because the latency is the same and I start to wait at the same time.

The first solution is what Microsoft suggest:
http://msdn.microsoft.com/en-us/library/hh696703.aspx

Solution1 MS suggestion

// call each thread and execute both and wait for eachone
var T1 = serviceClient.GetBusinessHours(SelectedStore.id);
var T2 = serviceClient.GetFlyerByStoreId(SelectedStore.id)

ListStoreHours = await T1();
ListFlyer = await T2();

This solution has the draw back that if the Task T2 is faster than T1 the ListFlyer will not be filled until T1 is completed so the user can’t view the result of T2 while waiting for T1.

Solution 2 using task WhenAll:

Task[] tasks = new Task[] {
                serviceClient.GetBusinessHours(SelectedStore.id).ContinueWith(
                    (TResult) => ListStoreHours = TResult.Result),
                serviceClient.GetFlyerByStoreId(SelectedStore.id).ContinueWith(
                    (TResult) => ListFlyer = TResult.Result)
                };

await Task.WhenAll(tasks);

Solution3 using Parallel

Parallel.Invoke(
    async () => ListFlyer = await serviceClient.GetFlyerByStoreId(SelectedStore.id),
    async () => ListStoreHours = await serviceClient.GetBusinessHours(SelectedStore.id)
);

Solution2 and Solution3 are both good solution choose what you prefer.
The main difference is that in when you use WhenAll solution all tasks were executed with the current thread, instead using Parallel the tasks were executed with different thread.

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in .Net, c# e contrassegnata con , , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...