[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding a message loop in Delphi

Posted on 2009-02-13
5
Medium Priority
?
819 Views
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?

Thanks
SF
0
Comment
Question by:sheepfarmer
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
btframework earned 2000 total points
ID: 23632431
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
private:
  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)
0
 
LVL 3

Assisted Solution

by:btframework
btframework earned 2000 total points
ID: 23632448
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
0
 

Author Comment

by:sheepfarmer
ID: 23632776
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));

Thanks
SF
0
 
LVL 3

Assisted Solution

by:btframework
btframework earned 2000 total points
ID: 23633307
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.
0
 

Author Comment

by:sheepfarmer
ID: 23633695
Thanks a lot.

SF
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question