Esempio n. 1
0
        private void RunDownloadSync()
        {
            var websites = PrepData();

            foreach (string site in websites)
            {
                WebsiteDataModel ws = DownloadWebsite(site);
                ReportWebsiteInfo(ws);
            }
        }
Esempio n. 2
0
        private WebsiteDataModel DownloadWebsite(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = client.DownloadString(websiteURL);

            return(output);
        }
Esempio n. 3
0
        private async Task <WebsiteDataModel> DownloadWebsiteAsync(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = await client.DownloadStringTaskAsync(websiteURL);

            return(output);
        }
Esempio n. 4
0
        private async Task RunDownloadAsync()
        {
            var websites = PrepData();

            foreach (string site in websites)
            {
                WebsiteDataModel ws = await Task.Run(() => DownloadWebsite(site));

                ReportWebsiteInfo(ws);
            }
        }
        public static List <WebsiteDataModel> RunDownloadSync()
        {
            var websites = PrepData();
            var output   = new List <WebsiteDataModel>();

            foreach (string site in websites)
            {
                WebsiteDataModel ws = DownloadWebsite(site);
                output.Add(ws);
            }

            return(output);
        }
        public static List <WebsiteDataModel> RunDownloadParallelSync()
        {
            var websites = PrepData();
            var output   = new List <WebsiteDataModel>();

            Parallel.ForEach <string> (websites, (site) =>
            {
                WebsiteDataModel ws = DownloadWebsite(site);
                output.Add(ws);
            });

            return(output);
        }
        public static async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress)
        {
            var websites = PrepData();
            var output   = new List <WebsiteDataModel>(); // Liste des sites à retourner
            var report   = new ProgressReportModel();     // Rapport de progression

            foreach (string site in websites)
            {
                WebsiteDataModel ws = await DownloadWebsiteAsync(site);

                output.Add(ws);                                                    // On ajoute le site terminé à la liste

                report.SitesDownloaded    = output;                                // On assigne la liste au rapport
                report.PercentageComplete = (output.Count * 100) / websites.Count; // Calcul du %

                progress.Report(report);                                           // On déclenche l'événement de rapportage
            }
            return(output);
        }
        public static async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress, CancellationToken cancellationToken)
        {
            var websites = PrepData();
            var output   = new List <WebsiteDataModel>();
            var report   = new ProgressReportModel();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await DownloadWebsiteAsync(site);

                output.Add(results);

                cancellationToken.ThrowIfCancellationRequested();

                report.SitesDownloaded    = output;
                report.PercentageComplete = (output.Count * 100) / websites.Count;

                progress.Report(report);
            }

            return(output);
        }
        public static async Task <List <WebsiteDataModel> > RunDownloadParallelAsyncV2(IProgress <ProgressReportModel> progress)
        {
            var websites = PrepData();
            var output   = new List <WebsiteDataModel>();
            var report   = new ProgressReportModel();

            await Task.Run(() =>
            {
                Parallel.ForEach <string>(websites, (site) =>
                {
                    WebsiteDataModel ws = DownloadWebsite(site);
                    output.Add(ws);

                    report.SitesDownloaded    = output;
                    report.PercentageComplete = (output.Count * 100) / websites.Count;

                    progress.Report(report);
                });
            });

            return(output);
        }
Esempio n. 10
0
        RunDownloadAsync(IProgress <ProgressReportModel> progress, CancellationToken cancellationToken)
        {
            var websites = PrepData();
            var output   = new List <WebsiteDataModel>();
            var report   = new ProgressReportModel();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await DownloadWebsiteAsync(site);

                output.Add(results);

                /// On déclenche l'exception OperationCanceledException
                /// SI DEMANDÉ
                cancellationToken.ThrowIfCancellationRequested();

                report.SitesDownloaded    = output;
                report.PercentageComplete = (output.Count * 100) / websites.Count;

                progress.Report(report);
            }

            return(output);
        }
Esempio n. 11
0
 private void ReportWebsiteInfo(WebsiteDataModel data)
 {
     resultsWindow.Text += $"{data.WebsiteUrl} downloaded :  {data.WebsiteData.Length} characters long. {Environment.NewLine}";
 }