Digital Garden
Computer Science
C#
Concurrency

Concurrency

   public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
// TASK IS FROZEN
        private void executeSync_Click(object sender, RoutedEventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
 
            RunDownloadSync();
 
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
 
            resultsWindow.Text += $"Total execution time: { elapsedMs }";
        }
 
        private async void executeAsync_Click(object sender, RoutedEventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
// Without await it would first print out time then all the sites
            await RunDownloadParallelAsync();
 
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
 
            resultsWindow.Text += $"Total execution time: { elapsedMs }";
        }
 
        private List<string> PrepData()
        {
            List<string> output = new List<string>();
 
            resultsWindow.Text = "";
 
            output.Add("https://www.yahoo.com");
            output.Add("https://www.google.com");
            output.Add("https://www.microsoft.com");
            output.Add("https://www.cnn.com");
            output.Add("https://www.codeproject.com");
            output.Add("https://www.stackoverflow.com");
 
            return output;
        }
 
        private async Task RunDownloadAsync()
        {
            List<string> websites = PrepData();
 
            foreach (string site in websites)
            {
                WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site));
                ReportWebsiteInfo(results);
            }
        }
 
        private async Task RunDownloadParallelAsync()
        {
            List<string> websites = PrepData();
            List<Task<WebsiteDataModel>> tasks = new List<Task<WebsiteDataModel>>();
 
            foreach (string site in websites)
            {
 
                // tasks.Add(DownloadWebsiteAsync(site));
                tasks.Add(Task.Run(()=>DownloadWebsite(site)))
            }
 
            var results = await Task.WhenAll(tasks);
 
            foreach (var item in results)
            {
                ReportWebsiteInfo(item);
            }
        }
 
        private void RunDownloadSync()
        {
            List<string> websites = PrepData();
 
            foreach (string site in websites)
            {
                WebsiteDataModel results = DownloadWebsite(site);
                ReportWebsiteInfo(results);
            }
        }
 
        private WebsiteDataModel DownloadWebsite(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient client = new WebClient();
 
            output.WebsiteUrl = websiteURL;
            output.WebsiteData = client.DownloadString(websiteURL);
 
            return output;
        }
 
        private async Task<WebsiteDataModel> DownloadWebsiteAsync(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient client = new WebClient();
 
            output.WebsiteUrl = websiteURL;
            output.WebsiteData = await client.DownloadStringTaskAsync(websiteURL);
 
            return output;
        }
 
        private void ReportWebsiteInfo(WebsiteDataModel data)
        {
            resultsWindow.Text += $"{ data.WebsiteUrl } downloaded: { data.WebsiteData.Length } characters long.{ Environment.NewLine }";
        }
    }
asyncTask<int> Delay1() { awaitTask.Delay(1000); return1; }
asyncTask<int> Delay2() { awaitTask.Delay(2000); return2; }
asyncTask<int> Delay3() { awaitTask.Delay(3000); return3; }
 
vart1 = Delay1();
vart2 = Delay2();
vart3 = Delay3();
 
// Include handling of results ↓↓↓ this takes 3s in total ↓↓↓
Console.WriteLine("{0} {1} {2}", awaitt1, awaitt2, awaitt3);
 
// Or, without handling the results:
await Task.WhenAll(t1, t2, t3);
Console.WriteLine("All done!");