How to implement Synchronous for async operation(WebAuthenticationBroker::AuthenticateAsync)?
-
Friday, June 01, 2012 10:29 AM
Hi all,
I want implement a function to do WebAuthenticationBroker::AuthenticateAsync()
When the AuthenticateAsync complete, it will set a value(bLogin) to True.
And show "Login Success"{ LoginLiveServer(); if(bLogin) OutputDebugString(L"Login Success "); else OutputDebugString(L"Login Fail "); } void CloudMediaMgr::LoginLiveServer() { auto LoginOp = WebAuthenticationBroker::AuthenticateAsync(WebAuthenticationOptions::None,
ref new Uri(ref new String(_oauthUri.c_str())), _callbackUri); LoginOp->Completed = ref new AsyncOperationCompletedHandler<WebAuthenticationResult^>([this]( IAsyncOperation<WebAuthenticationResult^>^ a, AsyncStatus b) { WebAuthenticationResult^ result = a->GetResults(); if (result->ResponseStatus == WebAuthenticationStatus::Success) { bLogin = true; // do something } }); }But the actual result is show "Login Fail"
I also try using task object like this
task<WebAuthenticationResult^> webAuthTask(WebAuthenticationBroker::AuthenticateAsync(WebAuthenticationOptions::None, ref new Uri(ref new String(_oauthUri.c_str())), _callbackUri)); webAuthTask.then([this](task<WebAuthenticationResult^> webAuthTask) { WebAuthenticationResult^ result = webAuthTask.get(); if (result->ResponseStatus == WebAuthenticationStatus::Success) { bLogin = true; } });
But the actual result is show "Login Fail", too
How to implement synchronous for this situation?Thanks
All Replies
-
Monday, June 04, 2012 8:37 AMModerator
Hello,
I was not clear your point, If you get the login fail, you can throw a error to stop the async function. You can follow this document to handle error in lambda function.
http://msdn.microsoft.com/en-us/library/windows/apps/dd997692(v=vs.110).aspx
Also, you can cancellation the lambda function to meet the error.
Please take a look Error Handling and Cancellation section in
http://msdn.microsoft.com/en-us/magazine/hh781020.aspx
Best regards,
Jesse
Jesse Jiang [MSFT]
MSDN Community Support | Feedback to us
-
Monday, June 04, 2012 11:28 AM
Hi Jesse,
Sorry, my English is bad.
My problem is "How to implement Synchronous for async operation"For my sample code, the LoginLiveServer() just call the WebAuthenticationBroker::AuthenticateAsync()
We assumption the WebAuthenticationBroker::AuthenticateAsync() that it always success loginFollowing this sample code.
bLogin = false; LoginLiveServer(); if(bLogin) OutputDebugString(L"Login Success "); else OutputDebugString(L"Login Fail ");
Because the WebAuthenticationBroker::AuthenticateAsync() is async operation
The task::then method returns immediately, and its delegate doesn't run until the AuthenticateAsync() work completes successfullySo, When the LoginLiveServer() have call.
The "bLogin" value is false. It doesn't change to true. It will show Text "Login Fail"
But after 5~10 sec, the WebAuthenticationBroker::AuthenticateAsync() it's complete.I just want know how to block the LoginLiveServer() function before the WebAuthenticationBroker::AuthenticateAsync() complete
Thanks
-
Monday, June 04, 2012 9:03 PMModerator
Are you calling this on a UI thread or on a worker thread?
If you are calling it on a worker thread then you can call task::wait to wait for the task to finish.
If you are on the UI thread then making the task synchronous may hang the UI: your users will be much happier if you don't do this. Instead of blocking waiting for the task to finish put the tasks which depend on the login in your completion function.
--Rob
- Marked As Answer by woody tk Wednesday, June 06, 2012 2:54 AM


