Recent

Author Topic: How to enter an idle state in console application?  (Read 1085 times)

Kurt

  • Jr. Member
  • **
  • Posts: 63
How to enter an idle state in console application?
« on: September 10, 2023, 05:53:06 pm »
I am writing a console application implementing a special web server.  I have outsmarted myself a little, in that it's entirely event driven, based on Indy 10 and TTimer events.  For any GUI application, it will happily sit and wait for events and I don't have to do anything.  I don't know how to do this for a console application.  I need a cross-platform idle do-nothing loop, and don't know the way to do that in FPC/Lazarus.

This thread suggests a loop repeatedly calling Application.ProcessMessages, but that's non-blocking so this will spike the CPU at 100%.  I don't want to Sleep the entire application.  All the threads are behind-the-scenes in Indy10, and I can't see how to sleep the main thread selectively.

What do visual applications do in their idle loop?

cdbc

  • Hero Member
  • *****
  • Posts: 1649
    • http://www.cdbc.dk
Re: How to enter an idle state in console application?
« Reply #1 on: September 10, 2023, 06:11:25 pm »
Hi
First off, take a look in TApplication, for the message-loop...
2.nd: what about:
Code: Pascal  [Select][+][-]
  1. ...
  2. var
  3.   Stop: boolean;
  4.   S: string;
  5. ...
  6.   // start your thread shenanigans ;-)
  7.   Stop:= false;
  8.   repeat
  9.     readln(S);
  10.     if S = 'q' then Stop:= true;
  11.   until Stop;
  12. ....
  13.  
Just my nickels worth  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Kurt

  • Jr. Member
  • **
  • Posts: 63
Re: How to enter an idle state in console application?
« Reply #2 on: September 10, 2023, 07:23:39 pm »
I was going to open some file and just read it past the end and let that block, and I forgot all about stdin.  I feel dumb now.

And yes, Use the Source.

Thanks for both reminders.

Kurt

  • Jr. Member
  • **
  • Posts: 63
Re: How to enter an idle state in console application?
« Reply #3 on: September 10, 2023, 07:50:51 pm »
Now that I've had 30 seconds to think about it, neither seem to work.

TApplication doesn't have it's own message loop.  TApplication.Run passes control to the current widget set's message loop.  Console applications have no widget set, so that does nothing. 

And stdin will get closed when the program runs as a daemon.  I could do as I originally thought, which is open up a random stream and just read it, but it seems inelegant.

EDIT: My thinking is to start a TThread, have that thread do nothing but TThread.Sleep(TM_FORBLOODYEVER), then have the main program Thread.WaitFor() on it. 
« Last Edit: September 10, 2023, 08:03:27 pm by Kurt »

cdbc

  • Hero Member
  • *****
  • Posts: 1649
    • http://www.cdbc.dk
Re: How to enter an idle state in console application?
« Reply #4 on: September 10, 2023, 08:12:40 pm »
Hi
How about entering a Criticalsection twice?!?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

rvk

  • Hero Member
  • *****
  • Posts: 6578
Re: How to enter an idle state in console application?
« Reply #5 on: September 10, 2023, 08:24:36 pm »
Can't you not just use the OnCommandGet for Indy?

See https://stackoverflow.com/a/27121006/1037511

alpine

  • Hero Member
  • *****
  • Posts: 1297
Re: How to enter an idle state in console application?
« Reply #6 on: September 10, 2023, 09:09:58 pm »
Daemons runs usually on Linux, so can't you just:
 
Code: Pascal  [Select][+][-]
  1. var
  2.   SigIntFlag: Boolean;
  3.  
  4. procedure DoSigInt(S: cint); cdecl;
  5. begin
  6.   SigIntFlag := True;
  7. end;
  8.  
  9.   ...
  10.   SigIntFlag := False;
  11.   FpSignal(SIGINT, @DoSigInt);
  12.   while not SigIntFlag do
  13.     Sleep(1000);
  14.   ...
  15.  
  16.  
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 360
  • My software never cras....
Re: How to enter an idle state in console application?
« Reply #7 on: September 10, 2023, 09:35:20 pm »
I am writing a console application implementing a special web server.  I have outsmarted myself a little, in that it's entirely event driven, based on Indy 10 and TTimer events.  For any GUI application, it will happily sit and wait for events and I don't have to do anything.  I don't know how to do this for a console application.  I need a cross-platform idle do-nothing loop, and don't know the way to do that in FPC/Lazarus.

Code: Pascal  [Select][+][-]
  1. Repeat
  2.      Sleep(100000);
  3. Until false;

F*** elegant.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: How to enter an idle state in console application?
« Reply #8 on: September 10, 2023, 09:53:19 pm »
How about simply using a WaitObject for the INPUT file ?

I believe that works with Winders console!!!!!
The only true wisdom is knowing you know nothing

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 87
Re: How to enter an idle state in console application?
« Reply #9 on: September 11, 2023, 01:53:13 am »
AHHH.... You are creating a TSR from the ancient days of DOS :D
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11936
  • FPC developer.
Re: How to enter an idle state in console application?
« Reply #10 on: September 19, 2023, 10:45:38 pm »
I usually use a .waitfor with a tthreadlist.

Put a message to process in the tthreadlist, and signal the condition variable. The thread wakes up, and drains the queue.

The trick would be how to get a shutdown event to signal the list.  On Windows this is easy by using waitmultiple variants.

On *nix that will be harder.

 

TinyPortal © 2005-2018