Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void DoChild(object state)
- {
- Console.WriteLine($"child {state} starting");
- Thread.Sleep(2000);
- Console.WriteLine($"Child {state} finished");
- }
- public static void ChildTasks()
- {
- var parent = Task.Factory.StartNew(() =>
- {
- Console.WriteLine("Parent Starts");
- //Child tasks can run independent of the parent or can be attached to parent
- //in the case of independent we can use Run method of task or explicitly state in
- //task creation option
- for(int i=0;i<10; i++)
- {
- int taskNo = i;
- Task.Factory.StartNew(
- (x) => DoChild(x),
- taskNo,
- TaskCreationOptions.AttachedToParent);
- }
- });
- parent.Wait();
- Console.WriteLine("Finished Processing. Press a key to end");
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement