none
Passing Parameter in Task.Factory.ContinueWhenAll method RRS feed

  • Question

  • I want to pass the parameter(Command3) to a Task which is created after completing the dependent tasks (task1 and task2).

            Task[] tasks = { task1, task2 };
            ICommand command3 = new TestCommand3();
            Task task3 = Task.Factory.ContinueWhenAll(tasks, (setOfTasks) =>
            {
                Console.WriteLine("Task3");
            });

    I do see the Task.Factory.StartNew and ContinueWith methods has that option and don't see parameter passing option available in ContinueWhenAll and ContinueWhenAny methods.

     Not sure why option parameter option not provided and want to know if there is any other better workaround option available.

    Saturday, November 25, 2017 5:25 AM

All replies

  • Have you tried to access command3 in the body of ContinueWhenAll?

    Saturday, November 25, 2017 6:08 PM
  • Hi TaskSparrows,

    You could execute command3's method directly. like this:

     Task[] tasks = { task1, task2 };
     ICommand command3 = new TestCommand3();
    Task task3 = Task.Factory.ContinueWhenAll(tasks, (setOfTasks) =>
     {
          command3.SayHello();
          Console.WriteLine("Task3");
     });

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 27, 2017 6:05 AM
    Moderator
  • I have a scenario where the tasks are created dynamically from a ForEach loop for each command. It might lead to race conditions when refer a outer variable inside the task function. 

    I tried wrapping with outer task, do you think any potential risk by doing so??

    Task[] tasks = { task1, task2 }; //dependent Tasks
    ICommand command = new TestCommand3();
    List<object> args = new List<object>();
    args.Add(command);
    args.Add(tasks);
    Task<string> wrapperTask = Task.Factory.StartNew((args) =>
    {
    var localCommand = //retreive it from args
    var tasks = //retrive it from args
    Task<string> innterTask = Task.Factory.ContinueWhenAll(tasks, (setOfTasks) =>
    {
    return localCommand.SayHello();
    });
    return innterTask.Result;
    }, args);

              
    Monday, November 27, 2017 7:51 PM
  • Hi TaskSparrows,

    According to your description and related code, I create a simple demo as below, it works.

    List<object> args = new List<object>();
    args.Add(command3);
    args.Add(tasks);
    Task<string> wrapperTask = Task.Factory.StartNew((command) => 
     {
          var localCommand = (ICommand)args[0]; //retreive it from args
         //var tasks = ""; //retrive it from args
         Task<string> innterTask = Task.Factory.ContinueWhenAll(tasks, (setOfTasks) =>
          {
                       return localCommand.SayHello();
          });
          return innterTask.Result;
    }, args);

    >>I tried wrapping with outer task, do you think any potential risk by doing so??

    I don't find any potential risk, because i could not find related I/O operation.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 28, 2017 5:53 AM
    Moderator