Introduction
Await & Async was built on the Task Parallel Library (TPL) which was introduced in the .NET Framework 4.
Their purpose is to enable asynchronous programming.
When you download some files on your mobile phone, your phone doesn't hang until it finishes downloading those files. You can still play Youtube videos or listen to Spotify. This is possible because the OS executes these tasks asynchronously.
In order to understand asynchronous programming, we need to have an example that includes a series of instructions.
Take a real-life example, let's make a pizza. Here is a list of steps involved in this process:
- Make and knead the pizza dough
- Prepare toppings: sliced chicken, sliced tomato
- Flatten a dough ball, then stretch and round it
- Spread with tomato sauce and sprinkle with toppings
- Slide the pizza onto the pizza stone in the oven
- Bake the pizza in the 475°F (245°C) oven, for about 10-15 minutes
Now, in order to make a delicious pizza, you don't have to follow the exact sequence of orders.
What you just did is execute the series of tasks asynchronously. You didn't follow the exact order as specified above.
We started to pre-head pizza stone after step 1, then the pizza stove will be running in the background without requiring your attention. Furthermore, we didn't wait until the pizza stone is completely heated. Rather, we moved forward with step 2 which is preparing toppings.
Here, the resource we are utilizing is time, we made some task run in the background. In the same way, in Computer Science we do asynchronous programming to utilizing resources such as Processor, RAM, etc. We are the character who makes the pizza, the same way in programming has Threads are which executes difference processes.
Let's code this out, Shall we? Because who understands theory?
We need some basic classes... I am not decorating classes, as our main focus is to understand asynchronous programming.
- class Dough
- {
- public void AddToppings(Toppings topping)
- {
- Console.WriteLine("Spread with tomato sauce and sprinkle with toppings");
- }
- }
- class Toppings
- {
- public string NameOfTopping { get; set; }
- }
- class PizzaStone { }
- class BakeThePizza { }
Now our actions: functions in programming:
Now the chef who is making a delicious pizza. Our Main method:
- private static Dough MakeDough()
- {
- Console.WriteLine(" 1: Make and knead the pizza dough, Divide the dough into two balls");
- return new Dough();
- }
- private static Toppings PrepareToppings(Toppings topping)
- {
- Console.WriteLine(" 6: Preparing a Topping" + topping.NameOfTopping);
- return new Toppings();
- }
- private static void FlattenDoughBall(Dough toast) =>
- Console.WriteLine(" 9: Flatten dough ball, and stretch out into a round");
- private static void AddToppings(Dough toast, Toppings topping)
- {
- toast.AddToppings(topping);
- }
- private static PizzaStone PreheatPizzaStone()
- {
- Console.WriteLine(" 3: Heating a stone started:");
- Console.WriteLine("-------------- Waiting for 5 seconds to Preheat the Pizza stone........----------------");
- Task.Delay(5000).Wait();
- Console.WriteLine(" 4: Heating a stone finished");
- return new PizzaStone();
- }
- private static BakeThePizza BackingPizza(Dough PreparedDough, PizzaStone heatedStone)
- {
- Console.WriteLine($" 11: Slide {PreparedDough} onto {heatedStone} in oven");
- Console.WriteLine(" 12: Bake pizza in the 475°F (245°C) oven");
- Console.WriteLine("--------------- Waiting for 10 seconds to taking pizza out and cut into 6 slices........------------");
- Task.Delay(10000).Wait();
- Console.WriteLine(" 13: Take a plate out");
- Console.WriteLine("---------------- Waiting for 3 seconds to garnish........-------------");
- Task.Delay(3000).Wait();
- Console.WriteLine(" 14: Serve the hot pizza");
- return new BakeThePizza();
- }
- static void Main(string[] args)
- {
- //Step - 1
- Dough dough = MakeDough();
- Console.WriteLine(" 2: Dough is ready");
- //Step - 5 but started now
- PizzaStone stone = PreheatPizzaStone();
- //Step - 2
- Toppings prepTopping = new Toppings() { NameOfTopping = "Tomato" };
- Console.WriteLine(" 5: Toppings are preapared");
- //Step - 3
- PrepareToppings(prepTopping);
- //Step - 4
- AddToppings(dough, prepTopping);
- Console.WriteLine(" 8: Toppings are");
- //Step - 5
- FlattenDoughBall(dough);
- Console.WriteLine(" 10: Dough has been flattern.");
- //Final : Eat
- BakeThePizza bakedPizza = BackingPizza(dough, stone);
- Console.WriteLine(" 14: Pizza is ready");
- Console.WriteLine("Eat, Sleep => Repeat!");
- }



Guest UserPosted Nov 22, 2022, 6:54 AM
Thank you for the article man. it helped me