locked
create_task is failing to succeed

    Question

  • StorageFolder^ AppDataLocal = ApplicationData::Current->LocalFolder;
    auto localPath = AppDataLocal->Path;

    //obtain static reference to the sub folder containing hints in AppData Local
    static StorageFolder^ hintFolder;

    Concurrency::create_task(AppDataLocal->GetFolderAsync(L"subfoldername")).then([](StorageFolder^ hintSubFolder)
    {
    String^ hintPath = hintSubFolder->Path;
    hintFolder = hintSubFolder;
    });


    auto QueryLocalOptions = ref new QueryOptions();
    if (hintFolder)
    {
    static auto localQueryResult = hintFolder->CreateFolderQuery();

    localQueryResult->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Platform::Object^>(this, &scenario::OnLocalAppDataChanged);


    localQueryResult->GetFoldersAsync();
    }
    else
    {
    DoTraceInfo(L"Local AppData Sub Folder has returned NULL");
    }

    }

    void scenario::OnLocalAppDataChanged(Windows::Storage::Search::IStorageQueryResultBase^ sender, Platform::Object^ args)
    {

    }

    All I'm doing is retrieving a subfolder inside appdata local and created a folder query for that. Now, my issue is most of the time my create_task is failing, i mean debugger is not getting into the chunk of the code at all, due that my subfolder reference is empty and i'm unable to use contents changed callback after that.

    But 1 or 2 times, I'm seeing it processed correctly (that is subfolder is retrieved, query passes and callback occurs on a content change). But why is that, i dont have it running every time? Is there some issue in my code? Can someone please immediately help me with this ?

    Wednesday, May 07, 2014 6:32 PM

Answers

  • Hi,

    In C++, this is a Lambda functions. And []meansspecifies which symbols visible in the scope where the function is declared will be visible inside the function body.

    A list of symbols can be passed as follows: 

    • [a,&b] where a is captured by value and b is captured by reference.
    • [this] captures the this pointer by value
    • [&] captures all automatic variables mentioned in the body of the lambda by reference
    • [=] captures all automatic variables mentioned in the body of the lambda by value
    • [] captures nothing

    You can refer to the link:

    http://en.cppreference.com/w/cpp/language/lambda

    In your code,[]and[this] can get the same result.

    And you can register this content handler inside a lambda body.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Minu7 Thursday, May 08, 2014 1:26 PM
    Thursday, May 08, 2014 8:54 AM

All replies

  • Hi,

    I test your code in my computer. I found eveytime i can run the codes successfully and the subfolder contentchanged event can fire when i add and delete file in it. So there may be no problem in your codes. I recommend you can create a simple project and define a create_Task method in it, if the create_Task method can also not run successfully, may be some problem contain in your develop environment. If the the create_Task method can run successfully but the codes you post do not run, you should check the "AppDataLocal->GetFolderAsync(L"subfoldername")" whether run successfully. If have some problem when run the code, your create_Task will failing.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 08, 2014 7:16 AM
  • Thanks Anne.. I tried something like this. My question now is can i register this content handler inside a lambda body ? And am new to using async tasks. What is difference between then([=]) and then([this]) ?

    StorageFolder^ AppDataLocal = ApplicationData::Current->LocalFolder;
    auto localPath = AppDataLocal->Path;
    auto subFolderPath = localPath + L"\\subfolder";

    //obtain static reference to the sub folder containing hints in AppData Local
    StorageFolder^ hintFolder;

    Concurrency::create_task(AppDataLocal->GetFolderAsync("subfolder")).then([this](StorageFolder^ hintSubFolder)
    //create_task(AppDataLocal->GetFolderFromPathAsync(subFolderPath)).then([](StorageFolder^ hintSubFolder)
    {
    if (hintSubFolder)
    {

    String^ hintPath = hintSubFolder->Path;
    auto QueryLocalOptions = ref new QueryOptions();
    static auto localQueryResult = hintSubFolder->CreateFolderQuery();


    localQueryResult->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Platform::Object^>(this, &scenario1::OnLocalAppDataChanged);


    //mandatory to call this atleast once, allows handler to hook on to the events
    localQueryResult->GetFoldersAsync();
    }
    else
    {
    //handle this
    }
    });


    • Edited by Minu7 Thursday, May 08, 2014 7:51 AM changed code
    Thursday, May 08, 2014 7:50 AM
  • Hi,

    In C++, this is a Lambda functions. And []meansspecifies which symbols visible in the scope where the function is declared will be visible inside the function body.

    A list of symbols can be passed as follows: 

    • [a,&b] where a is captured by value and b is captured by reference.
    • [this] captures the this pointer by value
    • [&] captures all automatic variables mentioned in the body of the lambda by reference
    • [=] captures all automatic variables mentioned in the body of the lambda by value
    • [] captures nothing

    You can refer to the link:

    http://en.cppreference.com/w/cpp/language/lambda

    In your code,[]and[this] can get the same result.

    And you can register this content handler inside a lambda body.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Minu7 Thursday, May 08, 2014 1:26 PM
    Thursday, May 08, 2014 8:54 AM
  • Thank you, this really helps.. Why is [this] and [] refers to the same in my code ? Can you also please answer the question in my other thread ?

    I'm currently calling a method that will register contents changed event handler in Page_Loaded call of my main page.

    Should i again register it in the suspension handler event ? CAn you show simple code to explain this ? it will be really helpful.

    Thank you in advance.

    Thursday, May 08, 2014 1:26 PM
  • CAn you please also tell me how to handle the previous task's exception ? I mean if my first task (getfolderasync fails), how would i handle that ? My app is crashing without that.
    Friday, May 09, 2014 6:34 AM