[SOLVED] Components with console applications lazarus / 21-General ---------------------------------------------------------------------- Kurt · 2023-09-09 16:30 Is there any way to use components with a console application the same way they are used with visual ones?  It would be nice to drop non- visual components on a container, and interact with them visually.  Or do I have to do everything programmatically? I suppose I could have a visual form that I instantiate but never actually display, but I need to ensure I do this in a way that the resulting app has no gui library dependencies on Linux (it may run on small console-only installations). ---------------------------------------------------------------------- JuhaManninen · 2023-09-09 18:54 Use a DataModule instead of a form for the non-visual components. Then set the widgetset of your LCL app to NoGUI. For example LazBuild is a NoGUI app because it depends on LCL through so many units. ---------------------------------------------------------------------- Kurt · 2023-09-10 15:53 Perfect!  It's actually too perfect in that it introduces an issue with waiting, but It's a separate issue so I'll post about it in a new thread. ---------------------------------------------------------------------- Kurt · 2023-09-10 19:05 Back to unsolved.  I can't get this to work.  I set up the DataModule and its components, but ttimer events aren't firing, some Indy stuff works because it's multi-threading and has its own timings, but a lot doesn't. It seems there is no message processing loop and I can't for the life of me figure out how to do one.  A console app with no widget set appears to have no message processing loop.  A console app derrives directly from TCustomApplication, so doesn't have TApplication.ProcessMessages. Is what I want to do possible? ---------------------------------------------------------------------- KodeZwerg · 2023-09-10 22:17 To have a window message loop you need ...... a window .... tadaaadaaa ---------------------------------------------------------------------- TRon · 2023-09-11 00:55 Quote from: Kurt on September 10, 2023, 09:05:26 pmBack to unsolved.  I can't get this to work.  I set up the DataModule and its components, but ttimer events aren't firing, some Indy stuff works because it's multi-threading and has its own timings, but a lot doesn't. KodeZwerg explained that better than I ever could. But... why would you want tot use timers in the first place. Such application that you speak about is event driven except not by using a standard windows message loop. You can create your own event loop and doing so in any which way is most convenient for you. So as a simple example for a timer you could create your custom "timer" class with for example an update event that is called in your custom event loop and each and every time update is executed you check the time, check it against your interval property and either fire an event that is attached to your custom timer class or post a (event) message to your custom loop (which your event loop would have to deal with accordingly). But for timed events I would probably just check against the current time and elapsed time in my custom event loop and when it matches, invoke the routine that needs to be run. It all depends on what you wish to achieve and how complicated you wish to make things. Another solution could be to use a thread instead. QuoteIs what I want to do possible? Sure, no problem. You just cant use any components that depend on the presence of a windows message loop (actually that is not entirely true because you could always resort to using a hidden window but in that case: what is the point). PS: if you want to see a custom (terminal based) loop in action you can take f.e. a look at FreeVision ---------------------------------------------------------------------- Kurt · 2023-09-13 20:39 Quote from: KodeZwerg on September 11, 2023, 12:17:29 amTo have a window message loop you need ...... a window .... tadaaadaaa  Well, I wasn't asking for a window message loop.  I was inquiring about a message loop. There is, as I've discovered, a very basic message handler in the nogui widget set.  It sorts out synchronization objects.  There is no reason why timer object support couldn't have been added to this paradigm.  I don't think it would even be that hard.  You don't need a widget set's window messaging system to support a timer.  You just need an OS, which I suspect all my apps users will have. Yes, I can easily use some other mechanism for my own timing needs.  I do it all the time in C.  That's not the problem.  My problem is that a bunch of non-visual components seem to themselves use TTimer to do things in the background or to handle their timeouts.  It's a little frustrating because you can't tell ahead of time which nonvisual component has problems unless you individually check the source for each one.  It would be nice if in the constructor for TTimer if it immediately tested the ability to actually time.  That would eliminate the guessing in about 90% of the cases of whether a non-visual component will work in nogui. I still like the idea of using Lazarus for my project, I think in the end it will make what I'm doing easier.  Right now, looking at the code and the ORBAT of components I'm looking at using, it seems like the shortest path to do that is to implement TTimer support in the nogui widget handler. ---------------------------------------------------------------------- alpine · 2023-09-14 11:50 @Kurt It may be not new for you, but LCL resembles VCL from the early days, which in turn originally was for Windows. The GUI is inseparable part and the console is a kind of compatibility feature on top of it. TTimer is implemented by means of a widget set (i.e. messages, callbacks, etc.). Not to mention the early Windows apps got no big notion of threads and were just a big message loop handlers. It is your decision if it's worth to artificially accommodate message loops in console just to have few properties into the designer or simply to reconsider not to use LCL components. IMHO it doesn't. You can hit other curbs very soon. Once I wrote a non visual component for protocol communication purposes. I had serious trouble using it in a daemon application, which also doesn't have  a normal message loop. The problem was TThread.Synchronize mechanism, et al. but there was a workaround for that. ---------------------------------------------------------------------- Kurt · 2023-09-16 00:38 Quote from: alpine on September 14, 2023, 01:50:32 pmor reconsider not to use LCL components.[/quote Maybe.  But it's the access to the LCL components that makes Lazarus attractive for this project - or rather attractive enough to investigate.  If I can't use those, then Lazarus is a liability and I'll just do it handraulically with a traditional Unix-style select() loop.  QuoteIMHO it doesn't. You can hit other curbs very soon. That may be true.  So far, thread synchronization seems to be the one part of the traditional message handling subsystem that nogui supports.  So far the biggest limitation to using other LCL components that I've found is their liberal use of TTimer.  But there may be other gotchas I haven't found yet. There seems to be a real missed opportunity.  Lazarus non-visual could have been what Go is today.  Of course, I'm sure everyone who finds Lazarus doesn't do what he wants it to do says it's a missed opportunity. Having admitted I'm aware of my own bias there, I still really wanna know who wrote noguiint.pp, got to writing CreateTimer() and said, "NOPE, can't fuckin' do that without GUI...  Result:=0 boyz".  Cuz I wanna raz that person just a bit. ---------------------------------------------------------------------- TRon · 2023-09-16 01:34 Quote from: Kurt on September 16, 2023, 02:38:08 amQuote from: alpine on September 14, 2023, 01:50:32 pmor reconsider not to use LCL components.[/quote Maybe.  But it's the access to the LCL components that makes Lazarus attractive for this project - or rather attractive enough to investigate.  If I can't use those, then Lazarus is a liability and I'll just do it handraulically with a traditional Unix-style select() loop.  QuoteIMHO it doesn't. You can hit other curbs very soon. That may be true.  So far, thread synchronization seems to be the one part of the traditional message handling subsystem that nogui supports.  So far the biggest limitation to using other LCL components that I've found is their liberal use of TTimer.  But there may be other gotchas I haven't found yet. There seems to be a real missed opportunity.  Lazarus non-visual could have been what Go is today.  Of course, I'm sure everyone who finds Lazarus doesn't do what he wants it to do says it's a missed opportunity. Having admitted I'm aware of my own bias there, I still really wanna know who wrote noguiint.pp, got to writing CreateTimer() and said, "NOPE, can't fuckin' do that without GUI...  Result:=0 boyz".  Cuz I wanna raz that person just a bit. Too time consuming to figure out who/what was quoted but have you considered the fact that that is probably why there exist this ? btw: also gitlab has something named history. You should definitely try it... ---------------------------------------------------------------------- jamie · 2023-09-16 01:57 Maybe all that is needed is a windows app but using nothing more than a TApplication instance. That gives you a message loop.  You can at any time create a form at runtime to display things.  A Timer may require to be sitting in a window, but that window does not need to be displayed. ---------------------------------------------------------------------- alpine · 2023-09-16 08:33 Quote from: jamie on September 16, 2023, 03:57:10 amMaybe all that is needed is a windows app but using nothing more than a TApplication instance. That gives you a message loop. Can you do that on a bare Linux with no GUI installed? ---------------------------------------------------------------------- JuhaManninen · 2023-09-19 17:52 Quote from: Kurt on September 16, 2023, 02:38:08 amHaving admitted I'm aware of my own bias there, I still really wanna know who wrote noguiint.pp, got to writing CreateTimer() and said, "NOPE, can't fuckin' do that without GUI...  Result:=0 boyz".  Cuz I wanna raz that person just a bit. I guess NoGUI widgetset was originally a hack to get LazBuild and maybe other programs to compile. Adding TTimer support would still be a good idea. It would extend its usefulness. I personally have tried to reduce dependencies and remove LCL dependency from LazBuild but it is super difficult. ---------------------------------------------------------------------- zeljko · 2023-09-19 19:07 Check TFreeTimer from here https://www.theo.ch/Kylix. It was maded for Kylix, but just replace Libc with BaseUnix and Unix (and fix relevant code here and there, not big problem) and you can use TFreeTimer as timer in console apps. ---------------------------------------------------------------------- TRon · 2023-09-19 20:35 Apparently you have to spell out things these days so there we go: TFPTimer does the (same) job *period* ---------------------------------------------------------------------- Kurt · 2023-09-24 23:04 Quote from: TRon on September 19, 2023, 10:35:34 pmTFPTimer does the (same) job *period* Oh, yes, I fully understand this.  My issue isn't that I can't figure out (or craft) a timer that works in my code within my project, my problem is that I've discovered that some component authors, even when making non-visual components, themselves used TTimer instead of TFPTimer.  In their defense, they probably weren't thinking about the widget set or that TTimer had no background support in nogui situations. The problem when using these components in a nogui environment is that they are a mine field.  Your project will compile, and often it will at least partially run with no errors.  It's only when one of the non- visual components you use itself tries to make a TTimer class go active that you get an exception.  And sometimes the functionality that makes the component do that is a fringe functionality that may not be obvious or even tested for. Anyway, rather than have to curate/port every non-visual component I ever use in nogui,  I updated the nogui interface with timer support in my installation.  It's interesting you brought up TFPTimer, since that's what I used (slightly modified) for the support.  Patches are attached in case anyone is interested.  It would have to be cleaned up to be used widely since I ruthlessly cheated with TFPTimer to make it work. ---------------------------------------------------------------------- Kurt · 2023-09-26 02:09 I found a way to do my main "message loop" without actually having to do anything. I had originally planned to periodically call WidgetSet.AppProcessMessages(), which for nogui just processes synchronize() on the main thread.  I discovered that if my main thread is waiting on another thread, then while its waiting any calls to synchronize() will interrupt the main thread and process immediately.  Automatically.  So I came up with a new sleep function I call LightSleep() Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit utilitiesunit;{$mode ObjFPC}{$H+} interface uses  Classes, SysUtils; type TWaitThread = class(TThread)protected  FDelay: integer;public  procedure Execute; override;  constructor Wait(delay: integer);end; procedure LightSleep(delay: integer); inline; implementation procedure TWaitThread.Execute;begin  Self.Sleep(FDelay);  Terminate;end; constructor TWaitThread.Wait(delay: integer);begin  inherited Create(true);  FDelay := delay;  Start();  WaitFor();end; procedure LightSleep(delay: integer); inline;begin    TWaitThread.Wait(delay).Free;end; end.  When the main thread is in a LightSleep(), any timers that fire (or any other calls to synchronize) will process immediately, almost as if there were a dedicated message handling loop. ---------------------------------------------------------------------- alpine · 2023-09-26 09:32 That's  an utterly mindless way to call CheckSynchronize(100): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TThread.WaitFor: Integer;begin  WRITE_DEBUG('waiting for thread ',ptruint(FHandle));  If (MainThreadID=GetCurrentThreadID) then     {     FFinished is set after DoTerminate, which does a synchronize of OnTerminate,      so make sure synchronize works (or indeed any other synchronize that may be      in progress)    }    While not FFinished do        CheckSynchronize(100);  WaitFor := WaitForThreadTerminate(FHandle, 0);  { should actually check for errors in WaitForThreadTerminate, but no }  { error api is defined for that function                             }  FThreadReaped:=true;  WRITE_DEBUG('thread terminated');end; Just call it without creating stillborn threads. ---------------------------------------------------------------------- Kurt · 2023-09-26 20:39 Quote from: alpine on September 26, 2023, 11:32:31 amThat's  an utterly mindless way to call CheckSynchronize(100): You're 100% right!  Lord, the Linux version of WaitFor() is the most asssuredly unfortunate way to implement that which one could think of.  I guess I should have checked it.  The Windows implementation is nicely done without any wheel spinning using WaitForMultipleObjects() and I just assumed the Linux version was properly implemented too. ---------------------------------------------------------------------- PascalDragon · 2023-09-28 20:56 Quote from: Kurt on September 26, 2023, 10:39:37 pmQuote from: alpine on September 26, 2023, 11:32:31 amThat's  an utterly mindless way to call CheckSynchronize(100): You're 100% right!  Lord, the Linux version of WaitFor() is the most asssuredly unfortunate way to implement that which one could think of.  I guess I should have checked it.  The Windows implementation is nicely done without any wheel spinning using WaitForMultipleObjects() and I just assumed the Linux version was properly implemented too. The problem is that Linux does not provide a good way to wait for both a thread handle and a pthread_cond. ---------------------------------------------------------------------- JuhaManninen · 2023-10-04 16:02 Quote from: Kurt on September 25, 2023, 01:04:45 amIt would have to be cleaned up to be used widely since I ruthlessly cheated with TFPTimer to make it work. If you figure out how to do it without changing FPC code, it could be applied to LCL NoGUI. ---------------------------------------------------------------------- Kurt · 2023-11-09 16:44 The project I intended this all for is complete.  This issue turned out to be moot, since Indy ripped out its use of TTimer. The project is AllCall, it's a cross-platform TCP/IP-over-http stream tunneler.  It's a client/server.  Client-side it acts as a local SOCKSv4/a server that your applications connect to.  It then talks to the AllCall server over your HTTP proxy and will work with virtually any HTTP proxy, no matter how basic (or restrictive).  It's beta quality now. Quote from: JuhaManninen on October 04, 2023, 06:02:06 pmIf you figure out how to do it without changing FPC code, it could be applied to LCL NoGUI. It's easily doable.  I just need to basically duplicate TFPTimer within nogui.  If that's not an issue, I'll make another patch.