I would like to have both a UWP xaml application (called my_xaml_app
) and a UWP Core application (called my_core_app
) running in the same program in their own respective thread.
So far I have tried the following approach:
my_core_app::my_core_app()
{
ViewProviderFactory viewProviderFactory;
winrt::Windows::ApplicationModel::Core::CoreApplication::Run(viewProviderFactory);
}
winrt::Windows::Foundation::IAsyncAction init_core_app()
{
co_await winrt::resume_background();
my_core_app coreApp;
}
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
init_core_app();
winrt::Windows::UI::Xaml::Application::Start(
[](auto &&)
{
winrt::make<winrt::my_xaml_app::implementation::App>();
});
return 0;
}
I didnt include the details of how my_core_app
and my_xaml_app
are instantiated but you can assume that they do work correctly since they launch correctly if I start them alone by themselves from the main thread.
In the code above, my_xaml_app
starts correctly because it is called from the main thread. However my_core_app
fails to start and reports the following errors:
WinRT originate error - 0x8001010E : 'The Application Object must initially be accessed from the multi-thread apartment.'.
The application called an interface that was marshalled for a different thread.
Microsoft C++ exception: winrt::hresult_wrong_thread at memory location 0x00000040E4EFE6E8
Is this something that is possible and if so how can it be done?
to initialize from main thread initially you can use CoreDispatcher
below is the sample code from the docs of running the app from CoreDispatcher
// App.cpp
...
// An implementation of IFrameworkView::Run.
void Run()
{
CoreWindow window{ CoreWindow::GetForCurrentThread() };
window.Activate();
CoreDispatcher dispatcher{ window.Dispatcher() };
dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
}
// The CoreApplication::Run call indirectly calls the App::Run function above.
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
CoreApplication::Run(App());
}
The error message is showing the internals of how COM apartments work. A single process can only have one MTA apartment. It is not possible to have two MTA threads in the same process.
That said, you can launch the application in another process and that would work.
User contributions licensed under CC BY-SA 3.0