I have a Delphi 6 application that is heavily multithreaded. I have a component I created that descends from TWinControl. When I first built it, I used a hidden window and it's WndProc to handle messages, allocated with AllocateHwnd(). Recently I started cleaning up the WndProc's in my code and decided to remove the auxiliary WndProc(). I changed the component to override the base class WndProc() method instead and do its custom windows message handling from there. In that WndProc() I called the inherited handler first and then processed my custom messages (WM_USER offsets), setting the message Result field to 1 if found one of my custom messages and handled it.
One important note. I put a line of code at the top of the WndProc() override that throws an Exception if the current thread id is not the VCL main thread. I wanted to make sure that the WndProc() only executed in the context of the main VCL thread.
After doing this and running my program I ran into something that seems truly bizarre. I ran my program as normal and did various tasks without error. Then, when I went to a TMemo control that resides on the same page as my TWinControl descendant. If I clicked inside that TMemo control the main thread check in my WndProc() override triggered. I had a breakpoint set on it and when I went to the call stack, there was nothing on it above my WndProc() override.
As far as I can tell, and I've double checked, I do not make explicit calls to the WndProc() override. That's not something I'd ever do. But given that my TWinControl component would have been created on the main VCL thread like all the other components, I can't fathom how the WndProc() override would ever execute in the context of a background thread, especially only when a UI action like a mouse click would happen. I understand how my WndProc() is tied to the TMemo control since all child windows hang off the top level window WndProc(), at least that's my understanding. But since all the component windows would have been created on the main VCL thread, then all their message queues should be executing in that context too, right?
So what kind of a situation could I have created to make my WndProc() run, and only sometimes, in the context of a background thread?