I am working on an C++ MFC/COM application (including VBA and .NET code) that needs to execute code on the COM STA thread from another thread.
One way, that works, is to use the PostMessage
function to post a message to a hidden HWND_MESSAGE
window, but it's quite slow (in my application it can take ~4ms to get onto the STA this way.
Instead using SendNotifyMessage
instead is significantly faster. In this case it takes < ~0.1ms to get onto the STA.
Due to the nature of the application and the number of calls that happen (outside of my control) this difference matters. Unfortunately I'm running into strange errors which are hard to pinpoint. Some might possibly relate to STA reentrancy, for example if some .NET code is pumping messages internally while blocking on a WaitHandle.WaitOne
I have noticed reentrancy. VBA is also involved.
My questions are:
- can
SendNotifyMessage
be used to get onto the STA thread, instead ofPostMessage
? - if so, is there a way to make it "play nice" with .NET? (making it non-blocking or using blocking non-pumping waits are not an option)
- (bonus) how does
SendNotifyMessage
actually get onto the STA thread so fast?