Hello @Shyam Butani ,
In WinUI or UWP, you cannot access UI elements on a non-UI thread because the UI elements can only be accessed from the UI thread.
Thank you.
Issues with Creating and Passing SolidColorBrush Objects Between Worker and UI Threads in WinRT/C++
Hello,
I'm working on a multi-threaded WinRT/C++ application and have encountered an issue when trying to create a SolidColorBrush object on a worker thread and pass its ABI pointer to the UI thread (main thread) for use in a UI element.
I’ve tried creating the SolidColorBrush object like this on the worker thread:
Color color = { 255, 255, 0, 0 };
SolidColorBrush brush(color);
However, this results in an error:
WinRT originate error - 0x8001010E : 'The application called an interface that was marshalled for a different thread.'. Microsoft C++ exception: winrt::hresult_wrong_thread at memory location 0x000000C837DFE908.
On the other hand, I am able to create an empty object on the worker thread, like this:
SolidColorBrush brush(nullptr); // or Brush brush(nullptr);
My question is: Am I missing something in my approach, or is there a known workaround for creating a SolidColorBrush object on the worker thread and passing it to the UI thread for use?
Additionally, if I create the SolidColorBrush object on the UI thread and get its ABI pointer using copy_to_abi, I find that converting it back to an object on the worker thread using copy_from_abi does not give the same object(values). I suspect this is due to the limitation mentioned earlier, where I can't create the object directly on the worker thread.
Any insights or suggestions on how to handle this would be greatly appreciated.
Thanks!
-
Junjie Zhu - MSFT 19,751 Reputation points Microsoft Vendor
2024-11-26T07:52:28.6933333+00:00