Recent

Author Topic: [SOLVED] TObject.Dispatch and message handling  (Read 293 times)

wittbo

  • Jr. Member
  • **
  • Posts: 65
[SOLVED] TObject.Dispatch and message handling
« on: August 19, 2019, 11:55:50 pm »
I'm just looking for an example, which demonstrates the method of sending messages to any objects -in my case this would be some forms- and how these forms could react to those messages. Especially the use of the TObject.dispatch procedure seems very intransparent to me.
Does anyone has some experience or has an example project for me?
Thanks in advance.
« Last Edit: August 20, 2019, 06:44:15 pm by wittbo »
-wittbo-
Lazarus 2.0.4  with FPC 3.0.4a
MBAir with MacOS 10.14.5
iMac with MacOS 10.13.6

mas steindorff

  • Sr. Member
  • ****
  • Posts: 426
Re: TObject.Dispatch and message handling
« Reply #1 on: August 20, 2019, 12:43:56 am »
to pass a message between forms or objects within the same program, just create a pubic function in each client and call.  perhaps you meant "between threads or programs"?
windows 7/10 - laz 2.0 / 1.2.6 general releases

jamie

  • Hero Member
  • *****
  • Posts: 2000
Re: TObject.Dispatch and message handling
« Reply #2 on: August 20, 2019, 03:52:57 am »
it's all smoke and mirrors

You declare a method in the class

MyMessage Procedure (Var M:TMessage) Message SOME_MESSAGE#?

-- you need to make the method of course like you do the others.

TForm1.MyMessage(Var:TMessage);
begin
 ….
End;

Use PostMessage(FOrm1.Handle, SOME_MESSAGE#, Optional, optional);

PascalDragon

  • Hero Member
  • *****
  • Posts: 639
  • Compiler Developer
Re: TObject.Dispatch and message handling
« Reply #3 on: August 20, 2019, 09:06:56 am »
@jamie: not quite if it's not a TForm descendant, but a simpler type.

@wittbo: maybe documentation can help you to understand it better?

wittbo

  • Jr. Member
  • **
  • Posts: 65
Re: TObject.Dispatch and message handling
« Reply #4 on: August 20, 2019, 11:13:05 am »
First, thanks to all, who answered.

And, slowly the smoke is clearing. But nevertheless, the whole topic seems to be very smoky.

For clarity:  I need a messaging method within one application only, one thread, not interapplication communication. Sounds very simple. One Window contains some settings, one or more other windows, which might be open at the same time, will display discrete contents depending on the settings of the settings window.

@PascalDragon:  I found this document this morning too; especially the section about Dispatch is not clear.

Until now, my sight is as follows:

1.  Each form, which should react to the Messages sent, has to define a message handler like this:
     procedure TForm1.MyHandler(Var Msg); message <msg_ID>;
2.  Invoking this handler may happen in two ways:
     2.1   Defining a message record for each different message globally and passing this record to the dispatch method of
             those Forms, which should react to this message. This means, that one has to address each form in the applications
             code separately.
    2.2    Use of Postmessage (...) or SendMessage(...) methods. I believe, that those methods address ALL of my application
             objects/classes automatically; only those will react, which have an appropriate message handler definition
             in their class definition (s.a.).

I will try both ways and post the corresponding projects into this thread.
-wittbo-
Lazarus 2.0.4  with FPC 3.0.4a
MBAir with MacOS 10.14.5
iMac with MacOS 10.13.6

wittbo

  • Jr. Member
  • **
  • Posts: 65
Re: TObject.Dispatch and message handling
« Reply #5 on: August 20, 2019, 06:43:53 pm »
OK. Everything is done. My testproject see below.

My conclusion is as follows:
- In every case (sendmessage, postmessage, dispatch) you will need one call for every object, you want to send a message
- sendmessage/postmessage are poor in that they cannot deliver pointers to data structures to your message handler, but they should be thread safe.
- dispatch is easy to use (no wparam, lparam needed); since you can define your own record passed to it (only the first element must be of type cardinal), you can define a record with a real pointer as the second element, so you can deliver the addrees of a data structure to your message handler. I really don't know, if dispatch is thread safe, but i feel, that it's not. It is more like a direct call to the message handler.

Ok. My initial problem is solved. Since my app is not multithreaded, I would prefer the dispatch method to have a way for delivering data to the message handler.
-wittbo-
Lazarus 2.0.4  with FPC 3.0.4a
MBAir with MacOS 10.14.5
iMac with MacOS 10.13.6