solomem / C-

0 stars 0 forks source link

Async and AWAIT #1

Open solomem opened 1 year ago

solomem commented 1 year ago

Asynchronous programming

Youtube tutorial Any program runs line by line, and one method has to wait for another to finish and return values.

Async is parallel.

image image

solomem commented 1 year ago

dog.txt

░░░░░░░░░▄░░░░░░░░░░░░░░▄ ░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌ ░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐ ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐ ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐ ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌ ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌ ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐ ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒ ◣_◢ ▒▒▒▀▄▌ ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌ ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐ ▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌ ▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐ ░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌ ░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐ ░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌ ░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀ ░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀ ░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀

solomem commented 1 year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAwaitDemo
{
    internal class Program
    {
        //Change the main method to return async task (void is returning anything)
        static async Task Main(string[] args)
        {
            string URL = "https://gist.githubusercontent.com/KenanY/7207204/raw/22563e813f1d1e4462db95786b524a16544bcedf/doge.txt";

            Stopwatch sw = new Stopwatch();

            sw.Start();

            var tasks = new List<Task> { SummonDogLocally(), SummonDogFromURL(URL) };
            await Task.WhenAll(tasks);

            sw.Stop();
            Console.WriteLine("We are done here... " + sw.Elapsed.TotalSeconds); 
        }

        // Async method has to return the Task
        // Returning a task is eqivelent to a <void> method
        // Here, we are printing the content directly inside the method.
        // If we need to return the string to the main method, we have to add the <string> to return the string

        static async Task SummonDogLocally()
        {
            Console.WriteLine("1. Summoning Dog Locally ...");

            //read all the data inside the txt file asyncly
            string dogText = await File.ReadAllTextAsync("dog.txt");

            Thread.Sleep(1000); // to show how the processes are done asynclly (in parallel)

            Console.WriteLine($"2. Dog Summoned Locally \n{dogText}");
        }
        static async Task<string> SummonDogLocallyString()
        {
            Console.WriteLine("1. Summoning Dog Locally ...");

            //read all the data inside the txt file asyncly
            string dogText = await File.ReadAllTextAsync("dog.txt");

            Console.WriteLine($"2. Dog Summoned Locally \n{dogText}");

            return dogText;
        }

        #region Summoning Dog From URL
        static async Task SummonDogFromURL(string URL)
        {
            Console.WriteLine("1. Summoning Dog From URL...");

            using(var httpClient = new HttpClient())
            {
                var response = await httpClient.GetStringAsync(URL);

                //From this line and below,
                //the execution will resume once the above awaitable is done
                //using await keyword, it will do the magic of
                //unwrapping the Task<string> into string (response variable)
                Console.WriteLine($"2. Dog Summoned Locally \n{response}");
            }
        }
        #endregion
    }
}