Application freezes after sending message to previously not shown frame

I have a PageControl with two TabSheets . Each of them has a Frame , let's call it Frame2 . I have a background thread that sends a Frame2 message when it has completed its task. Frame2 is on the second TabSheet , so it does not appear when the user launches the application.

The problem is that my application stops updating its content when the message sent by the thread is Frame2 , but only if Frame2 has not been displayed before. This makes me think that the message queue for Frame2 has not yet been initialized, and it is initialized when the Frame2 icon is first displayed on the screen. Am I guessing right?

Can someone give me some tips on how to initialize the message queue right after creating Frame2 so that it can listen to messages immediately?

+5
source share
1 answer

Not that there is no message queue in the frame — these are threads that have message queues rather than windows — but that the frame does not yet have a window handle. The window handle is most likely created only when the frame is first displayed, if you have not posted a message, in which case the window is created upon request.

If you try to send a message to it, you will probably have the following operator: PostMessage(Frame2.Handle, ...) .

Reading the component's Handle property will cause this component to create its own window if it does not already have a handle. When this happens in your secondary stream, a frame window is created from this secondary stream. This can lead to any number of problems along the line. Like all VCL windows, the frame window must belong to the main VCL stream.

Even if you make sure that the frame descriptor is created in the main thread before you send messages to it (for example, by calling HandleNeeded in the main thread), it is still possible that reading the frame Handle property will cause problems. This is because the VCL control can recreate its window. Then, once again, reading the Handle property may trigger the creation of a frame window in the wrong stream.

Safe technology is to frame AllocateHWnd to create a special message-only window. Do this in the frame constructor so that it is guaranteed in the main thread, and then post the messages there. When you create a window, you provide a callback method that will be called at any time when the window receives a message. This callback method must belong to the frame control so that it has access to all the fields and methods of the associated frame.

+10
source

All Articles