locked
Need help with creating condition based task continuations

    Question

  • I need to schedule two tasks based on a condition as given below:

    bool check();

    task<int> Function_1();

    task<int> Function_2();

    Main()

    {

    if(check)

    {

    Function_1()

    .then([](){

    //Do some work here

    return Function_2();

    })

    .then([](task<int> status){

    //Do some work here

    });

    }

    else

    {

    Function_2()

    .then([](task<int> status)

    {

    // Do some work here

    });

    }

    }

    What is the best way to write such code without any redundancy? Is there a way to attach a continuation with a task that executes based on a condition.? 

    I know that this can be possible if the first task is cancelled,  but we dont need to cancel that task.

    Thanks in advance for your help.

    Regards,

    Parul Gupta


    Parul Gupta

    Thursday, June 06, 2013 6:19 PM

Answers

  • Hello Parul Gupta,

    You could do something like this:

    auto cont1 = [](task<int> status) { // do some work return Function_2(); }; auto cont2 = [](task<int> status) { // do some work }; if (check()) { Function_1().then(cont1).then(cont2); } else { Function_2().then(cont2); }

    Hope this helps.

    Cheers


    Helge Mahrt - Microsoft Developer Support Escalation Engineer - If my reply answers your question, please mark this post as answered.


    Friday, June 07, 2013 9:28 AM