Advertisement
Guest User

Untitled

a guest
May 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System.Threading.Tasks;
  2.  
  3. namespace Async.Await
  4. {
  5.     public class Primerche
  6.     {
  7.         public async Task ExecuteMorningRoutine()
  8.         {
  9.             // Order coffee to the waiter.
  10.             var orderCoffeeTask = Task.Run(() => OrderCoffee(3000));
  11.  
  12.             // Read newspaper while he brings you coffee
  13.             // You are actually not being blocked here,
  14.             // because the OrderCoffee task is running asynchroniously.
  15.             var readNewspaperTask = Task.Run(() => ReadNewspaper(2500));
  16.  
  17.             // Stop reading newspaper, cause you got bored.
  18.             await readNewspaperTask;
  19.  
  20.             // Maximum time we will wait for the coffee,
  21.             // and actually being blocked, will be (3000 - 2500 = 500ms)
  22.             var coffee = await orderCoffeeTask;
  23.  
  24.             var drinkCoffeeTask = Task.Run(() => DrinkCoffee(coffee, 1000));
  25.             await drinkCoffeeTask;
  26.         }
  27.  
  28.         public async Task<Coffee> OrderCoffee(int timeToWaitInMilliseconds)
  29.         {
  30.             await Task.Delay(timeToWaitInMilliseconds);
  31.  
  32.             return new Coffee();
  33.         }
  34.  
  35.         public async Task ReadNewspaper(int timeInMilliseconds)
  36.         {
  37.             await Task.Delay(timeInMilliseconds);
  38.         }
  39.  
  40.         public async Task DrinkCoffee(Coffee coffee, int timeToDrinkInMilliseconds)
  41.         {
  42.             await Task.Delay(timeToDrinkInMilliseconds);
  43.         }
  44.     }
  45.  
  46.     public class Coffee
  47.     {
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement