Exemplo n.º 1
0
        //Don't return a void with async method!!!
        //Convention: Always add Async to method name that is async
        //if returning a string : async Task<string> RunDownloadASync()
        private async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress,
                                                                       CancellationToken cancellationToken)
        {
            List <string>           websites = model.Websites;
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();
            ProgressReportModel     report   = new ProgressReportModel();

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

                output.Add(results);

                // if (cancellationToken.IsCancellationRequested){break;}
                cancellationToken.ThrowIfCancellationRequested();

                report.SitesDownloaded    = output;
                report.PercentageComplete = (output.Count * 100) / websites.Count;
                progress.Report(report);
                // ReportWebsiteInfo(results);
            }
            return(output);
            //if returning a string :
            //return "Yep";
        }
Exemplo n.º 2
0
        protected async Task <WebsiteDataModel> DownloadWebsiteNativeAsync(string websiteUrl)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

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

            return(output);
        }
Exemplo n.º 3
0
        protected WebsiteDataModel DownloadWebsite(string websiteUrl)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

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

            return(output);
        }
Exemplo n.º 4
0
        private List <WebsiteDataModel> RunDownloadParallelSync()
        {
            List <string>           websites = model.Websites;
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            Parallel.ForEach <string>(websites, (site) => {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
            });
            return(output);
        }
Exemplo n.º 5
0
        private List <WebsiteDataModel> RunDownload()
        {
            List <string>           websites = model.Websites;
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            foreach (string site in websites)
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
            }
            return(output);
        }
Exemplo n.º 6
0
        private async Task <List <WebsiteDataModel> > RunDownloadParallelASyncV2(IProgress <ProgressReportModel> progress)
        {
            List <string>           websites = model.Websites;
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();
            ProgressReportModel     report   = new ProgressReportModel();

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

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

            return(output);
        }
Exemplo n.º 7
0
 protected void ReportWebsiteInfo(WebsiteDataModel data)
 {
     form.add2ListBox(data.WebsiteData.Length.ToString("0000000") + " characters long.  Downloaded: " + data.WebsiteUrl + Environment.NewLine);
 }