0

I am developing an Universal Windows App for Windows devices. I am developing the app in C++/CX.

In the App I want to check if a file exits on the device or not and call should be blocking call. So I wrote a function as shown below.

FileExist(String^ myFolder, String ^myFile)
{
    // Get the folder object that corresponds to myFolder
    // this absolute path in the file system.
    try{
    create_task(StorageFolder::GetFolderFromPathAsync(myFolder)).then([=]                      (StorageFolder^ folder){

           create_task(folder->GetFileAsync(name)).then([=](StorageFile^ myfile){
           return true;
            });
           return false;
    });
    }
    catch (Exception^ e)
    {
            return false;
    }
}

But the GetFolderFromPathAsync and GetFileAsync calls are asynchronous calls and my function should be blocking so I put a wait for each of these lambda. But I am getting following error.

“An invalid parameter was passed to a function that considers invalid parameters fatal.”

So someone please tell me how to make a blocking call for file presence for Universal Windows App.

1
  • What requires this to be in a blocking call?
    – Peter Torr
    Sep 13, 2015 at 20:35

1 Answer 1

1

If your method runs on the UI thread - you can't make it blocking because it's using async APIs and that would prevent the async calls from returning results, causing a deadlock. You can use the blocking get() method to wait for and get a task's result if running "in a task-based continuation.".

"In a Windows Store app, do not call wait in code that runs on the STA. Otherwise, the runtime throws concurrency::invalid_operation because this method blocks the current thread and can cause the app to become unresponsive. However, you can call the concurrency::task::get method to receive the result of the antecedent task in a task-based continuation."

https://msdn.microsoft.com/en-us/library/hh749955.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.