WinUI3 : Can we get Main threads DispatcherQueue from worker thread

Harshithraj1871 1,706 Reputation points
2022-12-08T13:25:18.08+00:00

Hi,

We were working on a multithreaded application where we wanted the UI Painting to be raised from different thread.

We came across DispatcherQueue, where we can get the Main threads dispatcher queue from Main thread, and then we can enqueue UI operations to main thread's DispatcherQueue object from different thread. For some reasons we don't want to pass on the main thread's DispatcherQueue object to worker/different threads.

Is there any way to access the main threads DispatcherQueue directly from worker thread, or post the UI operations to Main thread directly from the worker thread.

Thank you

Windows development | Windows App SDK
Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-12-09T15:39:17.533+00:00

    With this test, I update the UI from a worker thread :
    (thread created with _beginthreadex, changing the Background color of myButton every 1/2 second)

    268974-winui3-thread.gif

    At beginning

    #include <winrt\Microsoft.UI.Dispatching.h>_  
    #include <winrt\Windows.UI.Core.h>  
    

    Global variables

    HANDLE g_hThread = NULL;  
    HANDLE g_hEvent = NULL;  
    BOOL g_bContinue = TRUE;  
    int g_nCpt = 0;  
    winrt::Microsoft::UI::Xaml::Window::Closed_revoker m_closedRevoker;  
    

    Test on myButton_Click

    UINT idThread;  
    g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  
    if (g_hEvent)  
    {  
        g_bContinue = TRUE;  
        g_hThread = (HANDLE)_beginthreadex(NULL, 0, (unsigned int (WINAPI*)(void*))ChildThreadProc, (HANDLE)this, 0, &idThread);  
        SetEvent(g_hEvent);  
    }  
    

    Thread (remove space at S leep (editor bug))

     static UINT WINAPI ChildThreadProc(LPDWORD lpdwData)  
        {  
            DWORD dwWait;  
            while (g_bContinue)  
            {  
                dwWait = WaitForSingleObject(g_hEvent, INFINITE);  
                if (dwWait == WAIT_OBJECT_0)  
                {  
                    if (!g_bContinue)  
                        break;  
                    auto pThis = (MainWindow*)(lpdwData);  
                    bool isQueued = pThis->DispatcherQueue().TryEnqueue(  
                        Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal,  
                        [pThis]()  
                        {  
                            Microsoft::UI::Xaml::Media::SolidColorBrush blueBrush{ Windows::UI::Colors::Blue() };  
                            Microsoft::UI::Xaml::Media::SolidColorBrush redBrush{ Windows::UI::Colors::Red() };  
                            if (g_nCpt % 2 == 0)  
                            {  
                                pThis->myButton().Background(blueBrush);  
                            }  
                            else  
                            {  
                                pThis->myButton().Background(redBrush);  
                            }  
                            g_nCpt++;  
                        });  
                        S leep(500);  
                }  
            }  
            CloseHandle(g_hEvent);  
            return 0;  
        }  
    

    In Constructor

       m_closedRevoker = this->Closed(winrt::auto_revoke, [&](auto&&, auto&&)  
       {     
       		g_bContinue = FALSE;  
        	SetEvent(g_hEvent);  
       });  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2022-12-09T06:42:23.35+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You can spin off a worker thread carrying necessary information from the UI thread.
    Advanced concurrency and asynchrony with C++/WinRT: use winrt::resume_foreground and winrt::resume_foreground.

    Thank you.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.