Search code examples
delphidelphi-xe2monitoringmessage-queue

How to log/monitor selected WM_USER+n Windows messages?


Some dialogs and data modules in my code need to delay execution and do this by calling PostMessage(). This is not across modules, i.e. the datamodule will have the handler for the PostMessage issued there, so will a dialog.

There is (of course?) interaction between code in the different modules.

I want to trace in what order some specific messages are processed when my program runs.

(How) can I do this in Delphi XE2?

Everything runs in the main UI thread.


Additional notes:

  • Simply setting breakpoints does not work (I think). Some of these messages can be fired more than once (think 'updating tree list nodes towards the root') and I'm afraid to interfere when I stop on break points.
  • Example sequence: user starts code execution that ends with a PostMessage(X) in dialog, underlying Delphi events call third party software in datamodule that has to finish executing first, I do PostMessage(Y) to 'wait' until that is done. I suspect Ys being handled after X.

Solution

  • The cleanest way to do this is to log the messages. Logging allows you to inspect the operation of the program without disturbing it as can happen with interactive debugging breakpoints. And for message processing, as your have discovered, the disturbance of breakpoints makes understanding the flow rather difficult.

    Since you are posting the messages, they are handled by the thread's message queue. Assuming that the thread in question is the main UI thread, then you can use Application.OnMessage as your hook to perform logging. All queued messages that are handled by the VCL message loop pass through this event. It is usually best to use a TApplicationEvents instance to handle Application events.