Set MTA Thread on WinUI3

Khoa Bui 51 Reputation points
2022-07-06T10:31:38.17+00:00

Hi,
In winform, we have:

[MTAThread]  
 static void Main()  
 {  
      Application.Run(new Main());  
 }  

How can we set [MTAThread] in winUI3?

Thanks.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
734 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 11,661 Reputation points Microsoft Vendor
    2022-07-07T06:02:12.367+00:00

    You can write your own wWinMain by defining DISABLE_XAML_GENERATED_MAIN.

    int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)  
    {  
        {  
            void (WINAPI *pfnXamlCheckProcessRequirements)();  
            auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");  
            if (module)  
            {  
                pfnXamlCheckProcessRequirements = reinterpret_cast<decltype(pfnXamlCheckProcessRequirements)>(GetProcAddress(module, "XamlCheckProcessRequirements"));  
                if (pfnXamlCheckProcessRequirements)  
                {  
                    (*pfnXamlCheckProcessRequirements)();  
                }  
      
                ::FreeLibrary(module);  
            }  
        }  
      
        //instead of winrt::init_apartment(winrt::apartment_type::single_threaded);  
        winrt::init_apartment(winrt::apartment_type::multi_threaded);  
        ::winrt::Microsoft::UI::Xaml::Application::Start(  
            [](auto&&)  
            {  
                ::winrt::make<::winrt::Packaged::implementation::App>();  
            });  
      
        return 0;  
    }  
    
    1 person found this answer helpful.

  2. Xiaopo Yang - MSFT 11,661 Reputation points Microsoft Vendor
    2022-07-07T02:04:03.95+00:00

    It seems WinUI3 is implemented by MTA. You can see Application Class is applicable for Both ThreadingModel.
    Besides you can Indicate the threading model of a Windows Runtime class using ThreadingAttribute Or You can dispatch any thread using the Windows::System::Threading APIs, or use CoreWindow::CoreDispatcher from ASTA thread.

    0 comments No comments

  3. Trung Tran 1 Reputation point
    2022-07-07T03:45:14.94+00:00

    @Xiaopo Yang - MSFT

    I see that entry point of WinUI is auto generated

    How can I adjust it ton MTA?

    218452-image.png

    0 comments No comments

  4. Trung Tran 1 Reputation point
    2022-07-07T06:21:34.24+00:00

    @Xiaopo Yang - MSFT yeah, I see your solution. However, with winUI 3, I see that it has some different

    • We double click our Project and add define as attached picture

    218417-image.png

    • Then, we can override entry point - Main function into App.xaml..cs file. In this case, I use same with generated source code and update to MTAThreadAttribute
      218446-image.png

    Thank your support !

    0 comments No comments