Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Threading.Tasks;
- namespace Async.Await
- {
- public class Primerche
- {
- public async Task ExecuteMorningRoutine()
- {
- // Order coffee to the waiter.
- var orderCoffeeTask = Task.Run(() => OrderCoffee(3000));
- // Read newspaper while he brings you coffee
- // You are actually not being blocked here,
- // because the OrderCoffee task is running asynchroniously.
- var readNewspaperTask = Task.Run(() => ReadNewspaper(2500));
- // Stop reading newspaper, cause you got bored.
- await readNewspaperTask;
- // Maximum time we will wait for the coffee,
- // and actually being blocked, will be (3000 - 2500 = 500ms)
- var coffee = await orderCoffeeTask;
- var drinkCoffeeTask = Task.Run(() => DrinkCoffee(coffee, 1000));
- await drinkCoffeeTask;
- }
- public async Task<Coffee> OrderCoffee(int timeToWaitInMilliseconds)
- {
- await Task.Delay(timeToWaitInMilliseconds);
- return new Coffee();
- }
- public async Task ReadNewspaper(int timeInMilliseconds)
- {
- await Task.Delay(timeInMilliseconds);
- }
- public async Task DrinkCoffee(Coffee coffee, int timeToDrinkInMilliseconds)
- {
- await Task.Delay(timeToDrinkInMilliseconds);
- }
- }
- public class Coffee
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement