Answered How to get parent task's ID?

  • Saturday, December 19, 2009 4:13 AM
     
     
    Hello, there,

    Is there any way to get parent task's ID? The parent-child relationship has been established when they are created.

    Thanks.

All Replies

  • Wednesday, December 23, 2009 8:59 PM
     
     Answered
    Hi, Tao,

    There is no way to get a reference to the parent task from inside the child task, however, if you have full control of both the parent and child tasks, you can retrieve their individual Ids either by pulling their public Id property or calling the statis Task.CurrentId (which will return the id of the current running task) from the relevant task.

    Task parent = Task.Factory.StartNew(() =>
    {
        Console.WriteLine("Parent ID: {0}", Task.CurrentId);
        
        Task child = Task.Factory.StartNew(() =>
            {
                 Console.WriteLine("Child ID: {0}", Task.CurrentId);

             }, TaskCreationOptions.AttachedToParent);
        
        Console.WriteLine("Child ID: {0}", child.Id);

    });

    Console.WriteLine("Parent ID: {0}", parent.Id);

    Thanks
  • Monday, January 04, 2010 2:51 AM
    Owner
     
     
    Hi Tao-

    For what purpose do you need the parent ID?  I'm just looking to better understand the scenario that demands this.

    Thanks,
    Steve
  • Thursday, October 13, 2011 3:50 AM
     
     
    So, where is Parallel Tasks window (in debugger) getting "Parent" ID for a task?