Exception on Wait()

Dec 5, 2014 at 12:17 AM
Hi,

I am having an issue with using wait, get etc. Basically anything that makes an Async call sync.

I have a windows mobile 8.1 project in C#. We are using C++ for common functionality across platform (android, win8.1 and IOS). In Win8.1 we are using a Windows component run time project for the C++.

I can call my service access Async fine. Seems to work. As soon as i go method().wait() i get a invalid parameter error.

pplx::task<void> RestF::HTTPGetAsyncTST()
{
// I want to make the following HTTP GET: http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value
http_client client(U("http://api.flickr.com/services/rest/"));

uri_builder builder;
// Append the query parameters: ?method=flickr.test.echo&name=value
builder.append_query(U("method"), U("flickr.test.echo"));
builder.append_query(U("name"), U("value"));

auto path_query_fragment = builder.to_string();

// Make an HTTP GET request and asynchronously process the response
return client.request(methods::GET, path_query_fragment).then([](http_response response)
{
    // Display the status code that the server returned
    std::wostringstream stream;
    stream << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;
    std::wcout << stream.str();

    stream.str(std::wstring());
    stream << L"Content type: " << response.headers().content_type() << std::endl;
    stream << L"Content length: " << response.headers().content_length() << L"bytes" << std::endl;
    std::wcout << stream.str();

    auto bodyStream = response.body();
    streams::stringstreambuf sbuffer;
    auto& target = sbuffer.collection();

    bodyStream.read_to_end(sbuffer).get();

    stream.str(std::wstring());
    stream << L"Response body: " << target.c_str();
    std::wcout << stream.str();
});
}

RestF::RestF()
{
// THIS LINE GETS EXCEPTION!
HTTPGetAsyncTST().wait();
}

Any help would be greatly appreciated.
Kind regards,
Dec 5, 2014 at 12:20 AM
Edited Dec 5, 2014 at 12:42 AM
If i step through the "Wait()" code it generally works.

Is it around thread safety in Windows Runtime Component?

EXCEPTION:
Unhandled exception at 0x6C283C68 (msvcr120d_app.dll) in WindowsPhone.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
Coordinator
Dec 5, 2014 at 5:23 PM
Hi nate78,

Are you calling wait() on the UI thread? If so that could be your problem, in the WIndows Runtime waiting on a task throws an exception. If you instead hook up a continuation and process the result asynchronously does everything work fine.

Steve
Dec 5, 2014 at 8:30 PM
Thanks for the reply. No the wait is not in the ui. It is just in the calling method. This is in the windows runtime component and bitg are in the same class.
Coordinator
Dec 5, 2014 at 9:20 PM
Hi nate78,

Do you think you can narrow down to a case that reproduces the problem and share?

Thanks,
Steve
Dec 7, 2014 at 9:49 PM
I have a windows phone 8.1 app
it calls into a windows runtime component DLL
This method instantiates a class in the "shared" code portion

which runs the below code, and then the other code in the first post.
RestF::RestF()
{
// THIS LINE GETS EXCEPTION!

HTTPGetAsyncTST().wait();

}

What else can i provide that might help diagnose the issue?
Coordinator
Dec 8, 2014 at 5:36 PM
Hi nate78,

Is it possible for you to share your solution in a zip file with me? Or at least could you share the source code files? If you don't want to share on the forum you could directly email me at stgates at Microsoft dot com.

Thanks,
Steve