Answered by:
Collect result of an asynchronous operation in UI thread

Question
-
Hi,
I need to get the results of an asynchronous operation and I am calling this in UI thread. Calling task.get() in UI thread throws invalid_operation exception. My code looks like:
IAsncOperation<int> sendHttpRequestsAsync()
{
return create_async([]() ->task<int>
{
return create_task([]()
{
return ;
}
)
}
);
}
I am calling this function from UI thread as below
int func1()
{
int status = -1;
auto sendOp = SendHttpRequestsAsync();
// Here i need to set value of status based on my operation's results but this method returns before sendOp completes. If i create a task for it and do a get() operation thereafter, that throws an exception.
return status;
}
Please indicate the correct way of doing this.
Parul Gupta
Wednesday, June 26, 2013 6:51 PM
Answers
-
You can do a sendOp.then() and then retrieve the contents of the completion in the then completion handler. For example, take a look at this link: http://msdn.microsoft.com/en-us/library/windows/apps/Hh780559.aspx
If you are specifically looking for a sample that uses XmlHttpRequest2, you can get it off here: http://code.msdn.microsoft.com/HttpClient-sample-0c4d3c6c
The HttpRequest.cpp file has the DownloadAsync function that shows how you can retrieve the results of the async request.
Thanks,
Prashant.
@prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, Windows Store & Phone Developer Solutions
- Marked as answer by Jamles HezModerator Saturday, July 6, 2013 2:16 AM
Thursday, June 27, 2013 2:36 PMModerator -
Any time consuming operation should be execute into a different worker thread. Inside the delegate of worker thread you may call Dispatcher->RunAsync() in order to notify the UI thread.
auto taskDelegate = [=](Windows::Foundation::IAsyncAction^ workItem) { // code block for executing in worker thread Dispatcher->RunAsync(CoreDispatcherPriority::High, ref new DispatchedHandler( [=]() { // Again UI thread })); };
- Marked as answer by Jamles HezModerator Saturday, July 6, 2013 2:16 AM
Friday, June 28, 2013 2:15 PM
All replies
-
You can do a sendOp.then() and then retrieve the contents of the completion in the then completion handler. For example, take a look at this link: http://msdn.microsoft.com/en-us/library/windows/apps/Hh780559.aspx
If you are specifically looking for a sample that uses XmlHttpRequest2, you can get it off here: http://code.msdn.microsoft.com/HttpClient-sample-0c4d3c6c
The HttpRequest.cpp file has the DownloadAsync function that shows how you can retrieve the results of the async request.
Thanks,
Prashant.
@prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, Windows Store & Phone Developer Solutions
- Marked as answer by Jamles HezModerator Saturday, July 6, 2013 2:16 AM
Thursday, June 27, 2013 2:36 PMModerator -
Any time consuming operation should be execute into a different worker thread. Inside the delegate of worker thread you may call Dispatcher->RunAsync() in order to notify the UI thread.
auto taskDelegate = [=](Windows::Foundation::IAsyncAction^ workItem) { // code block for executing in worker thread Dispatcher->RunAsync(CoreDispatcherPriority::High, ref new DispatchedHandler( [=]() { // Again UI thread })); };
- Marked as answer by Jamles HezModerator Saturday, July 6, 2013 2:16 AM
Friday, June 28, 2013 2:15 PM