Question: C# In this lab, you will practice executing code synchronously and asynchronously. Using each approach, download a list of web pages and report how long
C#
In this lab, you will practice executing code synchronously and asynchronously. Using each approach, download a list of web pages and report how long the downloads took. You'll find that the asynchronous approach is much faster because it can contact all the sites simultaneously and wait for all the responses simultaneously.
Screenshot Output
AsyncLab.cs below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
namespace AsyncCSharp
{
class AsyncLab
{
//Downloads multiple sites synchronously and asynchronously
static void Main(string[] args)
{
AsyncLab p1 = new AsyncLab();
Console.WriteLine("----------SYNC----------");
p1.executeSync();
Console.WriteLine(" ----------ASYNC---------");
p1.executeAsync();
Console.ReadLine();
}
//STUDENT SECTION BEGIN============================================================================
//The start of the synchronous execution.
// Downloads a list of websites and reports the site data.
private void executeSync()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
//Insert Code Here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time elapsed: {0}ms", elapsedMs);
}
//The start of the asynchronous execution.
// Downloads a list of websites and reports the site data.
private async void executeAsync()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
//Insert Code Here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time elapsed: {0}ms", elapsedMs);
}
//Same thing as executeAsync, but allows you to loop through prep data
// and do things simultaneously
private async Task RunDownloadAsync()
{
//Insert Code Here
}
//STUDENT SECTION END==============================================================================
//Returns a list of websites; feel free to modify this list with your favorite sites!
private List
{
List
{
"https://www.yahoo.com",
"https://www.facebook.com",
"https://www.youtube.com",
"https://www.twitter.com",
"https://www.google.com",
"https://www.microsoft.com",
"https://www.cnn.com",
"https://www.codeproject.com",
"https://www.stackoverflow.com"
};
return sites;
}
//Accesses and downloads a website, returning the site data in a
// SiteData object (see the class below).
private SiteData DownloadSite(string websiteURL)
{
SiteData site = new SiteData();
WebClient client = new WebClient();
site.WebsiteUrl = websiteURL;
site.WebSiteData = client.DownloadString(websiteURL);
return site;
}
}
//Simple helper class to store a website's data
public class SiteData
{
public string WebsiteUrl { get; set; } = "";
public string WebSiteData { get; set; } = "";
public void ReportSiteData()
{
Console.WriteLine("Downloaded {0}.\t {1} characters long", WebsiteUrl, WebSiteData.Length);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
