New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async timeout support for C++/WinRT #341
Conversation
} | ||
auto wait_for(Windows::Foundation::TimeSpan const& timeout) const | ||
{ | ||
return impl::wait_for(static_cast<Windows::Foundation::IAsyncAction const&>(static_cast<D const&>(*this)), timeout); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impl:: [](start = 19, length = 6)
needed for disambiguation with current wait_for? (not used on wait_get)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. wait_for
calls impl::wait_for
but get
calls impl::wait_get
so no confusion. I didn't want to call it impl::get
and rather had the handful of impl functions start with wait_xxx
for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this, kennykerr!!! It is exactly what I was looking for when using SerialDevice! I do have a question regarding my implementation: |
@cScalpel Glad you like it! You'll need to check the specific information in the thrown exception to figure out what might be wrong ( try { ... } catch (hresult_error const& e)
{
printf("%ls\n", e.message().c_str());
} In general, we don't have expertise to answer questions about specific APIs. For that, I'd suggest Stack Overflow. Tag the question with |
Will do, the exception was indeed "The operation identifier is not valid." I'll update my question on stackoverflow. Thanks! |
Today C++/WinRT provides support for blocking wait via
async.get()
and cooperative wait viaco_await async
but there is not ability to use a timeout. This has lead some teams needing timeout support to implement various workarounds that are not very efficient or robust. Part 1 introduces support for a timeout for the blocking scenario. Part 2 introduces support for cooperative timeout for coroutines.The
get
function follows the pattern established bystd::future
so it makes sense to adopt future'swait_for
function to support a timeout as well. Note that WinRT only allows waiting once. That means you cannot usewait_for
in a loop, or any other scenario where multiple waits would occur.You can wait for an
IAsyncAction
as follows:You can wait for an
IAsyncOperation
as follows:Or more comprehensively: