private void PrintContentInTextBox(List <WebsiteInfo> result) { OperationWithTaskRun obj = new OperationWithTaskRun(); textBox1.Text = ""; foreach (WebsiteInfo item in result) { textBox1.Text += obj.FormatTheWebSiteContent(item); } MessageBox.Show(textBox1.Text); }
private async void AsyncCallBack_Click(object sender, EventArgs e) { OperationWithTaskRun obj = new OperationWithTaskRun(); List <WebsiteInfo> inputURLs = new List <WebsiteInfo>() { new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.google.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.yahoo.in" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.microsoft.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.stackoverflow.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.youtube.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.netflix.com" } }; List <Task <WebsiteInfo> > tasks = new List <Task <WebsiteInfo> >(); Stopwatch myWatch = Stopwatch.StartNew(); ///* // * This section is the arrangement is made for Progress and async task call back // */ Progress <ListOfWebsiteInfo> progressInfo = new Progress <ListOfWebsiteInfo>(); progressInfo.ProgressChanged += ProgressInfo_ProgressChanged; // var result = await obj.DownLoadWebsiteContentAsyncWithCallBack(inputURLs, progressInfo); // List <WebsiteInfo> newResult = new List <WebsiteInfo>(); newResult.AddRange(result); newResult.Add(new WebsiteInfo() { ContentLength = 10, URL = "Test", WebsiteContent = "Test" }); PrintContentInTextBox(newResult); // myWatch.Stop(); textBox1.Text = textBox1.Text + $"Total Time elapsed is {myWatch.ElapsedMilliseconds.ToString()}"; }
private async Task PrintWebSiteContentParallely() { OperationWithTaskRun obj = new OperationWithTaskRun(); List <WebsiteInfo> inputURLs = new List <WebsiteInfo>() { new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.google.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.yahoo.in" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.microsoft.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.stackoverflow.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.youtube.com" }, new WebsiteInfo() { ContentLength = 0, WebsiteContent = "", URL = "https://www.netflix.com" } }; List <Task <WebsiteInfo> > tasks = new List <Task <WebsiteInfo> >(); Stopwatch myWatch = new Stopwatch(); myWatch.Start(); foreach (WebsiteInfo item in inputURLs) { Task <WebsiteInfo> task = Task.Run <WebsiteInfo>(() => obj.DownLoadWebsiteContent(item)); //textBox1.Text += obj.FormatTheWebSiteContent(await task); //This will run things in parallel but loose the display one at a time in the text box.Refer calback from Async method implementation. tasks.Add(task); } inputURLs = new List <WebsiteInfo>(await Task.WhenAll(tasks)); foreach (WebsiteInfo item in inputURLs) { textBox1.Text += obj.FormatTheWebSiteContent(item); } myWatch.Stop(); textBox1.Text = textBox1.Text + $"Total Time elapsed is {myWatch.ElapsedMilliseconds.ToString()}"; }