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?

4

3 回答 3

3

这是一个非常好的问题,我不知道为什么人们会否决它。从我上来一个。

这不仅仅是永远等待的问题(而 checkforsomeevent do sleep(10); 将在 checkforsomeevent 检查您想要终止的任何事件时执行此操作,如果您不这样做,则为 TRUE。由于线程无事可做,因此睡眠(0)似乎浪费资源)

更大的问题是,Windows 将终止长时间运行的控制台程序,这些程序会在一段时间后初始化线程支持,因为它认为它们“挂起”。

我不完全确定这是在(墙上)运行时间或 CPU 资源上。

Free Pascal 帮助编译器 chmcmd 可以运行很长时间(几分钟,因为它正在压缩 html)。它支持 *nix 上的线程,但我从来没有让它在 Windows 下正常工作。(不可否认,由于 chmcmd 主要用于 *nix,它不是一个高优先级项目)

我当时研究了一下,看来您必须注册一个窗口句柄并处理消息以避免这种情况。我试过这个但失败了,并且找不到补丁 atm,它可能在我的工作系统上。

于 2012-12-30T20:30:32.383 回答
1

在 Linux 上,您可以调用pause()哪些块,直到收到信号。因此,您需要调用pause()is 一个循环。这比睡眠的优点是您的进程可以被终止信号终止。

于 2012-12-30T13:01:34.173 回答
0

如何创建 HTTP 服务器?如果您使用的是 TFPHTTPServer,则在明确销毁或崩溃之前,它永远不会关闭。

于 2012-12-30T13:07:13.177 回答