Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. public static void DoChild(object state)
  2. {
  3. Console.WriteLine($"child {state} starting");
  4. Thread.Sleep(2000);
  5. Console.WriteLine($"Child {state} finished");
  6. }
  7.  
  8. public static void ChildTasks()
  9. {
  10. var parent = Task.Factory.StartNew(() =>
  11. {
  12. Console.WriteLine("Parent Starts");
  13. //Child tasks can run independent of the parent or can be attached to parent
  14. //in the case of independent we can use Run method of task or explicitly state in
  15. //task creation option
  16. for(int i=0;i<10; i++)
  17. {
  18. int taskNo = i;
  19. Task.Factory.StartNew(
  20. (x) => DoChild(x),
  21. taskNo,
  22. TaskCreationOptions.AttachedToParent);
  23. }
  24. });
  25.  
  26. parent.Wait();
  27. Console.WriteLine("Finished Processing. Press a key to end");
  28. Console.ReadKey();
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement