[Lazarus] Using event driven components in console application

Bo Berglund bo.berglund at gmail.com
Tue Nov 3 14:21:27 CET 2015


I am moving a discussion that is being conducted in a thread named:
"Lazarus implementation of TListview etc?" to this new thread since
the topic has shifted away from the original....

This topic has to do with using communications components which fire
off "OnReceive" events when data arrives (on a serial line or a TCP
network connection) inside a console application.

I need to modify an existing Delphi class unit such that a serial port
component is replaced with a TCP client component. And it must work in
a console (non-GUI) application because it must run unattended via
cron.
Since the original serial component used a receive event for incoming
data the rather big class is geared towards handling incoming data
this way. So I need to find a TCP component that is non-blocking and
fires off OnReceive events when data arrives. Otherwise I will need to
do major rework in the class.

Long story winding through Synapse, ssockets and Indy10 finally
reached lNet today and initial tests make me think it is going to
work.

However there has been a number of issues raised as to the suitability
of threading in a console application in Lazarus/FPC so rather than
having this discussed in a thread seemingly about TListView I now
create this new thread.
In the old one I asked about creating my own events using a TThread
object doing blocking reads on a TIdTCPClient object and making the
connection between the two threads via CheckSynchronize() and
Synchronize() calls respectively.

Michael Schnell wrote:
>For the record:
>
>I verified that with the gtk2 (Linux) Widget type, "PostMessage()" uses 
>a different Event Queue ("TGTKMessageQueue") than "TThread.Queue()" and 
>TThread.Synchronize()".
>
>So for this, calling "CheckSynchronize() will not help. You need to use 
>the appropriate GUI-based Widget type.
>
>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.

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....


-- 
Bo Berglund
Developer in Sweden





More information about the Lazarus mailing list