using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleAdvancedTopics { public class ProcessingOrderAfterApproval // Thread Task { // Stuff here that runs with ProcessingOrderAferApproval // vars / methods / etc... /* === New Thread with 3 Tasks ======== */ // Thread and 2 Tasks // Task ProcessShippingDetails private to Parent Task private async Task ProcessShippingDetails(int orderId) { // Run() await Task.Run(() => { Console.WriteLine($"Starting shipping process for: {orderId}"); Thread.Sleep(4000); // ms Console.WriteLine($"Finished shipping process for: {orderId}"); }); // Run() } // Task ProcessShippingDetails // Task ProcessWarehouseDetails private to Parent Task private async Task ProcessWarehouseDetails(int orderId) { // Run() await Task.Run(() => { Console.WriteLine($"Starting warehouse process for: {orderId}"); Thread.Sleep(7000); // ms int stock = 0; // inventory 1 ok 0 outOfStock if (stock == 0) { // Out of Stock Console.WriteLine("Items out of stock, do something"); // capture the response from the api for the issue } Console.WriteLine($"Finished warehouse process for: {orderId}"); }); // Run() } // Task ProcessWarehouseDetails // Task ProcessOrder Parent Task public to the class public async Task ProcessOrder(int orderId) { Console.WriteLine($"Starting Parent Process for {orderId}"); // stuff here to do Thread.Sleep(3000); // Run the 2 Tasks Task shippingTask = ProcessShippingDetails(orderId); Task warehouseTask = ProcessWarehouseDetails(orderId); // continue with other stuff here... x amount of time Console.WriteLine("More stuff happening here..."); // Parent Task has to wait until all child tasks complete await Task.WhenAll(shippingTask, warehouseTask); Console.WriteLine($"Finished Parent Process for {orderId}"); } // Task ProcessOrder } // class } // namespace