Exemplo n.º 1
0
        private static async Task PlanPartySequentially()
        {
            Console.WriteLine("I'm planning a party!");
            var partyStatus = new PartyStatus();

            var timer = Stopwatch.StartNew();

            partyStatus.InvitesSent = await SendInvites();

            partyStatus.FoodCost = await OrderFood();

            partyStatus.IsHouseClean = await CleanHouse();

            Console.WriteLine($"Elapsed time: {timer.ElapsedMilliseconds}ms");
        }
Exemplo n.º 2
0
        private static async Task PlanPartyConcurrentlyFewerAwaits()
        {
            Console.WriteLine("Now I'm planning one with helpers working concurrently (again)!");
            var partyStatus = new PartyStatus();
            var timer       = Stopwatch.StartNew();

            var sendInvites = SendInvites();
            var orderFood   = OrderFood();
            var cleanHouse  = CleanHouse();

            await Task.WhenAll(sendInvites, orderFood, cleanHouse);

            partyStatus.InvitesSent  = sendInvites.Result;
            partyStatus.FoodCost     = orderFood.Result;
            partyStatus.IsHouseClean = cleanHouse.Result;

            Console.WriteLine($"Elapsed time: {timer.ElapsedMilliseconds}ms");
        }
Exemplo n.º 3
0
        private static async Task PlanPartyConcurrentlyWithoutWhenAll()
        {
            Console.WriteLine("Now I'm planning one with helpers working concurrently (no WhenAll)!");
            var partyStatus = new PartyStatus();
            var timer       = Stopwatch.StartNew();

            // kicks off all 3 tasks
            var sendInvites = SendInvites();
            var orderFood   = OrderFood();
            var cleanHouse  = CleanHouse();

            // waits for all 3 to complete
            partyStatus.InvitesSent  = await sendInvites;
            partyStatus.FoodCost     = await orderFood;
            partyStatus.IsHouseClean = await cleanHouse;

            Console.WriteLine($"Elapsed time: {timer.ElapsedMilliseconds}ms");
        }