private void RunDownloadSync() { List <string> websites = PrepData(); foreach (var site in websites) { WebsiteDataModel results = DownloadWebsite(site); ReportWebsiteInfo(results); } }
private async Task <WebsiteDataModel> DownloadWebsiteAsync(string websiteUrl) { var model = new WebsiteDataModel(); WebClient client = new WebClient(); model.WebsiteUrl = websiteUrl; model.WebsiteData = await client.DownloadStringTaskAsync(websiteUrl); return(model); }
private WebsiteDataModel DownloadWebsite(string websiteUrl) { var model = new WebsiteDataModel(); WebClient client = new WebClient(); model.WebsiteUrl = websiteUrl; model.WebsiteData = client.DownloadString(websiteUrl); return(model); }
private async Task RunDownloadASync() { List <string> websites = PrepData(); foreach (var site in websites) { //Task.Run() is used to wrap around code that you want to run asynchronous. //The await keyword tells the code to run the task asynchronously but wait for the result. //The point to do this is to let the let the UI-thread do its other jobs while a new thread takes care of the DownloadWebsite operation. WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site)); ReportWebsiteInfo(results); } }
private void ReportWebsiteInfo(WebsiteDataModel model) { resultsWindow.Text += $"{model.WebsiteUrl} downloaded: {model.WebsiteData.Length} characters long. {Environment.NewLine}"; }