Issues with Creating and Passing SolidColorBrush Objects Between Worker and UI Threads in WinRT/C++

Shyam Butani 330 Reputation points
2024-11-26T06:03:04.6433333+00:00

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!

Universal Windows Platform (UWP)
0 comments No comments
{count} vote

Accepted answer
  1. Junjie Zhu - MSFT 19,741 Reputation points Microsoft Vendor
    2024-11-26T07:52:28.6933333+00:00

    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.


0 additional answers

Sort by: Most helpful

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.