[Lazarus] Using event driven components in console application

Mattias Gaertner nc-gaertnma at netcologne.de
Tue Nov 3 14:55:54 CET 2015


On Tue, 03 Nov 2015 14:21:27 +0100
Bo Berglund <bo.berglund at gmail.com> wrote:

>[...]
> >Application.QueuAsyncCall() uses yet an additional Event Queue. Here 
> >using a WidgetType is not strictly necessary, but calling 
> >"CheckSynchronize() will not help, either. Here, TApplication provides 
> >"RemoveAsyncCalls()" which in a GUI Bases Widget Type is called in 
> >"Forms". As a non-GUI Project does not have Forms, RemoveAsyncCalls() 
> >needs to be called by a different functionality.
> 
> Others have pointed out that events will not work in a console
> application because there is no message loop built in.

There are different kinds of message loops.
For multithreading Synchronize and Thread.Queue you just need a loop in
the main thread that calls CheckSynchronize.

A LCL program calls Application.Run, which creates such a loop,
processing AsyncCalls, Idle calls and CheckSynchronize.
The NoGUI widgetset does that too.
The other widgetsets additionally support PostMessage and SendMessage,
implementing a more complex message queue. Post/SendMessage require
Handles, which the NoGUI widgetset does not have.

 
> But I have now tested the lNet TCP component created in code inside a
> console application and it seems to work, at least inside the Lazarus
> IDE under the debugger.
> But this is of course in a test appliocation built as a descendant to
> TCustomApplication and not yet using the Delphi class I need to
> transform....

lnet does not use Post/SendMessage, so it works even with the NoGUI
widgetset.

You can easily implement your own loop and simply call
Application.ProcessMessages.

Mattias




More information about the Lazarus mailing list