Esempio n. 1
0
            private static async Task <bool> QueryAsync(ITapServices svc, String question)
            {
                Uri[] voters = await svc.GetVotersAsync(question);

                List <Task <bool> > tasks = new List <Task <bool> >();

                CancellationTokenSource source = new CancellationTokenSource();

                int agree         = 0;
                int numberOfVotes = 0;

                for (int i = 0; i < voters.Length; i++)
                {
                    tasks.Add(svc.GetAnswerAsync(voters[i - 1], question, source.Token));
                }

                for (int i = 0; i < voters.Length; i++)
                {
                    var task = await Task.WhenAny(tasks);

                    tasks.Remove(task);
                    agree += task.Result ? 1 : 0;

                    if (agree > voters.Length / 2 || numberOfVotes - agree > voters.Length / 2)
                    {
                        break;
                    }
                }

                source.Cancel();

                await Task.WhenAll(tasks);

                return(agree > voters.Length / 2);
            }
Esempio n. 2
0
 public TapApiService(IApiUserProvider <MyBeerTapApiUser> userProvider)
 {
     if (userProvider == null)
     {
         throw new ArgumentNullException("userProvider");
     }
     _userProvider = userProvider;
     _tapService   = new TapServices();
 }