6

In a Linux Free Pascal 2.6.0 console application, a HTTP server is started and runs in a separate thread, so the call to Start will immediately return.

begin
  ...
  MyHTTPServer.Start;
  ...
  WriteLn('Application terminated');
end;

To keep the console from closing I could use a simple endless loop like:

// wait, read and ignore input from stdin
while True do ReadLn;

or

// Sleep as long as possible
while True do Sleep(MaxInt);

Which one would you prefer? Or is there a better way to keep the application running?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
mjn
  • 36,362
  • 28
  • 176
  • 378
  • not sure if this is helpful http://wiki.freepascal.org/Daemons_and_Services ... otherwise both loops could do the same thing – cristi _b Dec 30 '12 at 12:37

3 Answers3

3

That is a very good question, and I don't know why people voted it down. One up from me.

It is not just a matter of waiting forever ( while checkforsomeevent do sleep(10); will do that where checkforsomeevent checks for whatever event you want to terminate on, or TRUE if you don't. Since the thread has nothing to do, sleep(0) seems a waster of resources)

The bigger problem is that Windows will terminate long running console programs that initialize threading support after a while because it thinks they "hang".

I'm not entirely sure this is on (wall) running time, or CPU resources though.

The Free Pascal help compiler chmcmd can run a long time (minutes, because it is compressing html). It supports threading on *nix, but I never got it to work properly under Windows. (and admittedly, since chmcmd is mostly used on *nix, it was not a high priority item)

I back then researched a bit, and it seems you must register a windowhandle and process messages to avoid this. I tried this but failed, and can't find the patch atm, it is probably on my work system.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
1

On Linux you can call pause() which blocks until a signal is received. You'd therefore need to call pause() is a loop. The advantage of this over sleeping is that you process can be terminated by a termination signal.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

How do you create the HTTP server? If you're using TFPHTTPServer it should never close until explicitly destroyed or crash.

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38