In our project we group multiple machines and request information from each one. Sometimes this takes very long.
All machines are contacted in sequence. When one machine is offline the request will timeout in 10 seconds (default WCF behavior) This way the user must wait very long when some machines are offline. The graph below shows the messages captured with fiddler when one machine is offline. Look for the exclamation mark.
We could call the services asynchronous but that would mean handling events and messing up the code.
Using tasks we now call the services in parallel. In the code sample below are the old and new sources.
// old code foreach(var machine in machines) CallTheService(machine); // new code var tasks = machines.Select(machine => Task.Factory.StartNew(() => CallTheService(machine)) ).ToArray(); Task.WaitAll(tasks);
This way we don’t reduce the wait for an offline machine, but make better use of the idle time by doing other requests. The graph below shows the messages captured with fiddler of the same group of machine as the graph above. The total process took 16 seconds in stead of 35.
When a user has to wait it is always to long. Make sure you do the best you can to reduce the wait.