Skip to content

Commit 4e32538

Browse files
authored
winui dispatcher (#648)
1 parent d6a0412 commit 4e32538

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

cppwinrt/code_writers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,10 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
31683168
{
31693169
w.write(strings::base_coroutine_system);
31703170
}
3171+
else if (namespace_name == "Microsoft.System")
3172+
{
3173+
w.write(strings::base_coroutine_system_winui);
3174+
}
31713175
else if (namespace_name == "Windows.UI.Core")
31723176
{
31733177
w.write(strings::base_coroutine_ui_core);

cppwinrt/cppwinrt.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<ClInclude Include="..\strings\base_coroutine_system.h" />
5757
<ClInclude Include="..\strings\base_coroutine_threadpool.h" />
5858
<ClInclude Include="..\strings\base_coroutine_ui_core.h" />
59+
<ClInclude Include="..\strings\base_coroutine_system_winui.h" />
5960
<ClInclude Include="..\strings\base_deferral.h" />
6061
<ClInclude Include="..\strings\base_delegate.h" />
6162
<ClInclude Include="..\strings\base_error.h" />

cppwinrt/cppwinrt.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@
157157
<ClInclude Include="..\strings\base_includes.h">
158158
<Filter>strings</Filter>
159159
</ClInclude>
160+
<ClInclude Include="..\strings\base_coroutine_system_winui.h">
161+
<Filter>strings</Filter>
162+
</ClInclude>
160163
</ItemGroup>
161164
<ItemGroup>
162165
<ResourceCompile Include="$(OutDir)version.rc" />
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
WINRT_EXPORT namespace winrt
3+
{
4+
[[nodiscard]] inline auto resume_foreground(
5+
Microsoft::System::DispatcherQueue const& dispatcher,
6+
Microsoft::System::DispatcherQueuePriority const priority = Microsoft::System::DispatcherQueuePriority::Normal) noexcept
7+
{
8+
struct awaitable
9+
{
10+
awaitable(Microsoft::System::DispatcherQueue const& dispatcher, Microsoft::System::DispatcherQueuePriority const priority) noexcept :
11+
m_dispatcher(dispatcher),
12+
m_priority(priority)
13+
{
14+
}
15+
16+
bool await_ready() const noexcept
17+
{
18+
return false;
19+
}
20+
21+
bool await_resume() const noexcept
22+
{
23+
return m_queued;
24+
}
25+
26+
bool await_suspend(std::experimental::coroutine_handle<> handle)
27+
{
28+
return m_dispatcher.TryEnqueue(m_priority, [handle, this]
29+
{
30+
m_queued = true;
31+
handle();
32+
});
33+
}
34+
35+
private:
36+
Microsoft::System::DispatcherQueue const& m_dispatcher;
37+
Microsoft::System::DispatcherQueuePriority const m_priority;
38+
bool m_queued{};
39+
};
40+
41+
return awaitable{ dispatcher, priority };
42+
};
43+
44+
#ifdef __cpp_coroutines
45+
inline auto operator co_await(Microsoft::System::DispatcherQueue const& dispatcher)
46+
{
47+
return resume_foreground(dispatcher);
48+
}
49+
#endif
50+
}

0 commit comments

Comments
 (0)