c++-winrtwindows-10-desktopwinui-3

WinUI3/Desktop/C++: Can't compile winrt::resume_foreground(Microsoft::System::DispatcherQueue const& dispatcher)


I want to use:

co_await winrt::resume_foreground(window.DispatcherQueue());

(Type of "window" is: "winrt::Microsoft::UI::Xaml::Window")

But I cannot compile it because

winrt::resume_foreground(Microsoft::System::DispatcherQueue const& dispatcher)

is not defined.

I cannot include #include <winrt/Microsoft.System.h> which contains the DispatcherQueue class.

My environment:
Windows 10 Pro, 21H1, 19043.1083
Visual Studio Community 2019 (16.10.3)
Visual Studio Extension: Project Reunion Version 0.8.0.46122163
Project Template: C++, Blank App, Packaged (WinUI 3 in Desktop)

To reproduce the error I used the Project Template above and added the following method to the "App" class.

App.xaml.h

winrt::Windows::Foundation::IAsyncAction foo();

App.xaml.cpp

winrt::Windows::Foundation::IAsyncAction App::foo()
{
    co_await winrt::resume_foreground(window.DispatcherQueue());
}

I get the error message:

D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,21): error C2039: 'resume_foreground': is not a member of 'winrt'
1>D:\Solution\WinUi3 Test\WinUi3 Test\MainWindow.xaml.h(23): message : see declaration of 'winrt'
1>D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,38): error C3861: 'resume_foreground': identifier not found
1>Done building project "WinUi3 Test.vcxproj" -- FAILED.

If I try to include #include <winrt/Microsoft.System.h> I get:

1>D:\Solution\WinUi3 Test\WinUi3 Test\pch.h(25,10): fatal error C1083: Cannot open include file: 'winrt/Microsoft.System.h': No such file or directory
1>Done building project "WinUi3 Test.vcxproj" -- FAILED.

If I include #include <winrt/Windows.System.h> and #include <winrt/Windows.UI.Core.h> I get:

1>D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,63): error C2665: 'winrt::resume_foreground': none of the 2 overloads could convert all the argument types
1>D:\Solution\WinUi3 Test\WinUi3 Test\Generated Files\winrt\Windows.UI.Core.h(2805,31): message : could be 'winrt::resume_foreground::awaitable winrt::resume_foreground(const winrt::Windows::UI::Core::CoreDispatcher &,const winrt::Windows::UI::Core::CoreDispatcherPriority) noexcept' (compiling source file App.xaml.cpp)
1>D:\Solution\WinUi3 Test\WinUi3 Test\Generated Files\winrt\Windows.System.h(4529,31): message : or       'winrt::resume_foreground::awaitable winrt::resume_foreground(const winrt::Windows::System::DispatcherQueue &,const winrt::Windows::System::DispatcherQueuePriority) noexcept' (compiling source file App.xaml.cpp)
1>D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,63): message : while trying to match the argument list '(winrt::Microsoft::UI::Dispatching::DispatcherQueue)'
1>Done building project "WinUi3 Test.vcxproj" -- FAILED.

Solution

  • Include

    #include <winrt/Microsoft.UI.Dispatching.h>
    #include <Microsoft.UI.Dispatching.co_await.h>
    

    When updating from 0.8.0 preview to 0.8.0 there was a namespace change from Microsoft.System to Microsoft.UI.Dispatching and resume_foreground is now defined in Microsoft.UI.Dispatching.co_await.h.