0

I'm working on some library code for WinUI 3 applications, and I want to assert that certain functions are called on the UI thread.

I know how to check if the current thread is the UI thread for UWP, but how do I do that for WinUI 3? CoreDispatcher and CoreWindow aren't used the same in WinUI 3.

citelao
  • 4,898
  • 2
  • 22
  • 36

1 Answers1

0

Use DispatcherQueue::GetForCurrentThread instead of the CoreWindow.GetForCurrentThread().Dispatcher method for UWP. It will return a dispatcher instance if you're on the UI thread, & nullptr otherwise:

bool IsUIThread()
{
    return nullptr != winrt::Microsoft::UI::Dispatching::DispatcherQueue::GetForCurrentThread();
}
citelao
  • 4,898
  • 2
  • 22
  • 36
  • 1
    Will this work both for UWP applications and WinUI3? – Ari Krumbein Aug 03 '23 at 21:19
  • That's an interesting question---I don't know offhand. But I think the `Microsoft::UI` namespace is unique to WinUI 3, so it won't be available for UWP or WinUI 2 apps. – citelao Aug 03 '23 at 21:23