示例#1
0
        private WebsiteDataModel DownloadWebsite(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = client.DownloadString(websiteURL);
            return(output);
        }
示例#2
0
        private void RunDownloadSync()
        {
            List <string> webSites = PrepData();

            foreach (string site in webSites)
            {
                WebsiteDataModel results = DownloadWebsite(site);
                ReportWebsiteInfo(results);
            }
        }
示例#3
0
        private void RunDownloadParallelSync(IProgress <ProgresReportModel> progress, CancellationToken ct)
        {
            List <string>           webSites = PrepData();
            ProgresReportModel      report   = new ProgresReportModel();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            Parallel.ForEach <string>(webSites, (site) =>
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
                ct.ThrowIfCancellationRequested();

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

                progress.Report(report);
            });
            PrintResults(output);
        }
示例#4
0
        private async Task RunDownloadAsync(IProgress <ProgresReportModel> progress, CancellationToken ct)
        {
            List <string>           webSites = PrepData();
            ProgresReportModel      report   = new ProgresReportModel();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

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

                output.Add(results);

                ct.ThrowIfCancellationRequested();

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

                progress.Report(report);
            }
        }
示例#5
0
 private void ReportWebsiteInfo(WebsiteDataModel data)
 {
     resultsTextBlock.Text += $"{data.WebsiteUrl} downloaded: {data.WebsiteData.Length} characters long: {Environment.NewLine}";
 }