public async Task <R> ExecOnNearServerAsync_4 <S, R>(ITAPServices <S, R> svc, Uri[] servers, S service)
 {
     Task <Uri>[] tasks = new Task <Uri> [servers.Length];
     for (int i = 0; i < servers.Length; ++i)
     {
         tasks[i] = svc.PingServerAsync(servers[i]);
     }
     Task.Factory.ContinueWhenAll(tasks, (antecedents) => {
         try { Task.WaitAll(antecedents); } catch { /*** log any thrown exceptions ***/ }
     });
     return(await svc.ExecServiceAsync(await Task.WhenAny(tasks).Result, service));
 }
        public Task <R> ExecOnNearServerAsync <S, R>(ITAPServices <S, R> svc, Uri[] servers, S service)
        {
            Task <Uri>[] pingTasks = new Task <Uri> [servers.Length];
            for (int i = 0; i < servers.Length; ++i)
            {
                pingTasks[i] = svc.PingServerAsync(servers[i]);
            }
            return(Task.Factory.ContinueWhenAny(pingTasks, (ant) =>
            {
                pingTasks = pingTasks.Where(t => t != ant).ToArray();
                Task.Factory.ContinueWhenAll(pingTasks, (ant2) =>
                {
                    try
                    {
                        Task.WaitAll(ant2);
                    }
                    catch { }
                });

                return svc.ExecServiceAsync(ant.Result, service);
            }).Unwrap());
        }
        public async Task <R> ExecOnNearServerAsync_5 <S, R>(ITAPServices <S, R> svc, Uri[] servers, S service)
        {
            Task <Uri>[] pingTasks = new Task <Uri> [servers.Length];
            for (int i = 0; i < servers.Length; ++i)
            {
                pingTasks[i] = svc.PingServerAsync(servers[i]);
            }
            do
            {
                Task <Uri> pingTask = await Task.WhenAny(pingTasks);

                pingTasks = pingTasks.Where(t => t != pingTask).ToArray();
                try
                {
                    Uri uri = pingTask.Result;
                    Task.Factory.ContinueWhenAll(pingTasks, (ant) =>
                    {
                        try
                        {
                            Task.WaitAll(ant);
                        }
                        catch
                        {
                        }
                    });
                    return(await svc.ExecServiceAsync(uri, service));
                }
                catch (AggregateException ae)
                {
                    if (pingTasks.Length == 0)
                    {
                        throw ae.InnerException;
                    }
                }
            } while (true);
        }