locked
Wait for chain of tasks

    Question

  • I am working with the Magazine sample app for using DirectWrite from a XAML-based app. It has a FontLoader class, which has the LoadAsync method, which does something like this:

    void FontLoader::LoadAsync()
    {
        // Locate the "fonts" sub-folder within the document folder
        task<StorageFolder^> getFolder(m_location->GetFolderAsync("Fonts"));
        getFolder.then([=](StorageFolder^ folder)
        {
            // Enumerate a list of .TTF files in the storage location
            auto filters = ref new Platform::Collections::Vector<Platform::String^>();
            filters->Append(".ttf");
    
            auto queryOptions = ref new QueryOptions(CommonFileQuery::DefaultQuery, filters);
            auto queryResult = folder->CreateFileQueryWithOptions(queryOptions);
    
            return queryResult->GetFilesAsync();
    
        }).then([=](IVectorView<StorageFile^>^ files)
        {
            // Here is some more async processing code.
        });
    }

    How can I make a Sync version of this method and wait for the whole chain to finish? I basically want to wait until all the async tasks here (also the tasks inside the second continuation) are completed.

    I tried to call getFolder.wait() at the end of the function, but that threw an exception.

    Thanks.

    Thursday, March 15, 2012 3:35 PM

Answers

  • Hello,

    If you call the getFolder.wait();. It only run the function getFolder not the getFolder.then().

    I suggest you to use .wait() at the end of then() like:
    getFolder.then().then().wait();

    Also, you can use .wait() inside the
    then([=](IVectorView<StorageFile^>^ files)
        {
            // Here is some more async processing code.
        });

    For more information, please check
    http://msdn.microsoft.com/en-us/library/windows/apps/dd492427(v=vs.110).aspx

    Best regards,
    Jesse


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

    • Marked as answer by MarkVincze Wednesday, March 21, 2012 10:54 AM
    Tuesday, March 20, 2012 3:13 AM

All replies

  • Dear Mark

    I had a same problem with you and look forward somebody to answer it.

    Friday, March 16, 2012 2:00 AM
  • Hello,

    If you call the getFolder.wait();. It only run the function getFolder not the getFolder.then().

    I suggest you to use .wait() at the end of then() like:
    getFolder.then().then().wait();

    Also, you can use .wait() inside the
    then([=](IVectorView<StorageFile^>^ files)
        {
            // Here is some more async processing code.
        });

    For more information, please check
    http://msdn.microsoft.com/en-us/library/windows/apps/dd492427(v=vs.110).aspx

    Best regards,
    Jesse


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

    • Marked as answer by MarkVincze Wednesday, March 21, 2012 10:54 AM
    Tuesday, March 20, 2012 3:13 AM