Exemplo n.º 1
0
        async Task AccessTheWebAsync(CancellationToken ct)
        {
            try
            {
                HttpClient    client  = new HttpClient();
                List <string> urlList = UrlBuilder.SetUpURLList(Convert.ToInt32((this.ViewModel.EndDate - this.ViewModel.StartDate).TotalDays), ViewModel.Table, ViewModel.CurrencyCode, ViewModel.StartDate);
                List <Task <List <RateModel> > > downloadTasks = (from url in urlList select WebServiceConsumer.ProcessURL(url, client, ct, () => this.ViewModel.SetErrorText("Error"))).ToList();
                this.ViewModel.ShowProgressBars();
                this.ViewModel.SetProgressInterval(Convert.ToInt32(Math.Ceiling(100D / downloadTasks.Count())));
                while (downloadTasks.Count > 0)
                {
                    Task <List <RateModel> > firstFinishedTask = await Task.WhenAny(downloadTasks);

                    downloadTasks.Remove(firstFinishedTask);
                    await firstFinishedTask.ContinueWith((t) =>
                    {
                        var res = t.Result;
                        this.ViewModel.CurrenciesSet(Randomizer.RandomArrayEntries(res, 10));
                        this.ViewModel.UpdateProgress();
                    }, TaskScheduler.FromCurrentSynchronizationContext());
                }
                this.ViewModel.HideProgress();
            }
            catch (AggregateException aggex)
            {
                this.ViewModel.HideProgress();
                this.ViewModel.SetErrorText("Connection can not be established");
            }
            catch (Exception ex)
            {
                this.ViewModel.HideProgress();
                this.ViewModel.SetErrorText("Connection can not be established");
            }
        }