Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Last active January 5, 2018 14:46
Show Gist options
  • Save kennykerr/341be078f87e871edc6e8c2ad5608e92 to your computer and use it in GitHub Desktop.
Save kennykerr/341be078f87e871edc6e8c2ad5608e92 to your computer and use it in GitHub Desktop.
A complement to C++/WinRT's resume_background coroutine helper
#include <winrt/Windows.UI.Core.h>
namespace winrt
{
struct resume_foreground
{
explicit resume_foreground(Windows::UI::Core::CoreDispatcher&& dispatcher, Windows::UI::Core::CoreDispatcherPriority const priority = Windows::UI::Core::CoreDispatcherPriority::Normal) :
m_dispatcher(std::move(dispatcher)),
m_priority(priority)
{
}
explicit resume_foreground(Windows::UI::Core::CoreDispatcher const& dispatcher, Windows::UI::Core::CoreDispatcherPriority const priority = Windows::UI::Core::CoreDispatcherPriority::Normal) :
m_dispatcher(dispatcher),
m_priority(priority)
{
}
bool await_ready() const
{
return m_dispatcher.HasThreadAccess();
}
void await_resume() const noexcept
{
}
void await_suspend(std::experimental::coroutine_handle<> handle) const
{
m_dispatcher.RunAsync(m_priority, [handle]
{
handle();
});
}
private:
Windows::UI::Core::CoreDispatcher const m_dispatcher;
Windows::UI::Core::CoreDispatcherPriority const m_priority;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment