We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Adding a message loop in Delphi

sheepfarmer asked
Medium Priority
Last Modified: 2012-08-13
I am looking to implement some sort of message or callback within a Delphi comms program.

I have a main form and a secondary form containing all the comms stuff.

I call a function on the comms form to send a comms request.
The comms form sends the message for me and has a receive event that collected the received information (which can take 5-10 seconds)

I need some method of the comms form letting the main form that something has been received on the comms port.

What would be an appropriate way to do this?

Watch Question

Well. The simplest solution is just to add message handling to the main form. Let say, your thread will send message SendMessage(MainForm.Handle, WM_USER+10, WPARAM(DataPointer), LPARAM(DataLength));
then add to the main form
  procedue WMUser10(var Msg: TMessage); message WM_USER + 10;
and handle all you need in routines.

But it is better to create thread for the comms because, as you wrote, it takes some time. As soon as Delphi runs all forms in the same main thread your app will hangs untill your comm receive data (or course, if it is synchronious)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
The other way is to add event to the comm form and when the comms data will be availbale the comm form will fire the event and the main form will handle it. This is much better and simplest solution


Indeed, I have implemented your second suggestion.
The comms form does have an event for 'data received' (using the ComPort library) so when that it's fired, I gather the data I need, then send a message to the main form.

What is the different between SendMessage and PostMessage (I am using the latter as I found that via separate research).

Also, I am using 0 for DataPointer and DataLength as I only need to know when there is data received (I collect the data from the comms form via a direct function call).  Presumably its perfectly valid to just send
SendMessage(MainForm.Handle, WM_USER+10, 0,0));

Send message is synchronous operation. The calling thread will wait until target thread process message. The PostMessage is asynchronous one. It returns immediatelly after call but the message placed in the thread's message queue.For your application it actually does not matter which one you will use.


Thanks a lot.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.