//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"; }
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); }
protected WebsiteDataModel DownloadWebsite(string websiteUrl) { WebsiteDataModel output = new WebsiteDataModel(); WebClient client = new WebClient(); output.WebsiteUrl = websiteUrl; output.WebsiteData = client.DownloadString(websiteUrl); return(output); }
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); }
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); }
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); }
protected void ReportWebsiteInfo(WebsiteDataModel data) { form.add2ListBox(data.WebsiteData.Length.ToString("0000000") + " characters long. Downloaded: " + data.WebsiteUrl + Environment.NewLine); }