locked
what the different form create_async return task and void

    Question

  • In the document, http://msdn.microsoft.com/en-us/library/windows/apps/hh750082.aspx

    It said that Conversely, if you return a taskobject, the work function runs synchronously. Therefore, if you return a task object, ensure that any lengthy operations in your work function also run as tasks to enable your app to remain responsive.

    So, What the different when we use task.then function in WinRT between the create_async return void?


    NEU_ShieldEdge

    Sunday, May 13, 2012 11:08 AM

Answers

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 11:24 AM
  • create_async is an API which is used to produce a WinRT asynchronous operation (IAsyncAction / IAsyncOperation<T> with or without progress) from C++ which can be consumed in a cross-language way from either C++ or JavaScript.  .then schedules a continuation which runs (often on the calling thread) when the task on the left side of the .then call is completed.

    When you call create_async and provide a void returning lambda such as shown below:

    IAsyncAction^ asyncAction = create_async( [](){
        //
        // BODY OF CODE
        //
    });

    the "BODY OF CODE" will be run on a background thread and the create_async call will return an IAsyncAction^ representing that asynchronous body of code.  The returned IAsyncAction^ can be passed between languages and consumed via constructs such as await in C#.

    If a lambda provided to create_async happens to return a task, the returned task itself is the body of the asynchronous work and the lambda runs synchronously.  This can allow you to compose multiple operations together.  For instance (please excuse the pseudo-code here):

    IAsyncOperation<T>^ asyncOperation = create_async( []() {
        return create_task(FirstAsync(...))
        .then( [](X val){
            return SecondAsync(val, ...);
        }).then( [](Y val)
            return ThirdAsync(val, ...);
        });
    });

    When you make this call, the lambda will run synchronously -- creating a task to encapsulate FirstAsync and then returning a cotinuation off that chain.  Despite the fact that the lambda here runs synchronously, it's purpose is simply to define the asynchronous operation which is going to occur (in this case a continuation chain of FirstAsync->SecondAsync->ThirdAsync).  The resulting IAsyncOperation<T>^ returned from this create_async call represents the asynchronous return value of ThirdAsync.

    I hope that helps.

    Monday, May 14, 2012 10:15 PM

All replies

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 11:24 AM
  • create_async is an API which is used to produce a WinRT asynchronous operation (IAsyncAction / IAsyncOperation<T> with or without progress) from C++ which can be consumed in a cross-language way from either C++ or JavaScript.  .then schedules a continuation which runs (often on the calling thread) when the task on the left side of the .then call is completed.

    When you call create_async and provide a void returning lambda such as shown below:

    IAsyncAction^ asyncAction = create_async( [](){
        //
        // BODY OF CODE
        //
    });

    the "BODY OF CODE" will be run on a background thread and the create_async call will return an IAsyncAction^ representing that asynchronous body of code.  The returned IAsyncAction^ can be passed between languages and consumed via constructs such as await in C#.

    If a lambda provided to create_async happens to return a task, the returned task itself is the body of the asynchronous work and the lambda runs synchronously.  This can allow you to compose multiple operations together.  For instance (please excuse the pseudo-code here):

    IAsyncOperation<T>^ asyncOperation = create_async( []() {
        return create_task(FirstAsync(...))
        .then( [](X val){
            return SecondAsync(val, ...);
        }).then( [](Y val)
            return ThirdAsync(val, ...);
        });
    });

    When you make this call, the lambda will run synchronously -- creating a task to encapsulate FirstAsync and then returning a cotinuation off that chain.  Despite the fact that the lambda here runs synchronously, it's purpose is simply to define the asynchronous operation which is going to occur (in this case a continuation chain of FirstAsync->SecondAsync->ThirdAsync).  The resulting IAsyncOperation<T>^ returned from this create_async call represents the asynchronous return value of ThirdAsync.

    I hope that helps.

    Monday, May 14, 2012 10:15 PM
  • Thanks all.

    NEU_ShieldEdge

    Thursday, May 17, 2012 12:01 PM
  • Just wanted to add that the create_task Bill used is not present in the beta and to use it, you have to download it as part of the async samples update (ppltasks_extra.h). I assume it will be part of the next update or RTM.


    http://blog.voidnish.com

    Saturday, May 19, 2012 3:11 PM