Delphi 2007 在 httpServerCommandGet 中使用 TIdHTTPServer 从数据模块更新控制台应用程序

问题描述 投票:0回答:1

我们有以下类,我们希望使用 Indy 从数据模块的过程中更新控制台。

   TLog = class(TIdNotify)
   protected
      FMsg: string;
      procedure DoNotify; override;
   public
     class procedure LogMsg(const AMsg : string);
   end;


   procedure TLog.DoNotify;
   begin
     writeln(fMsg); //this is probably completely wrong
   end;

   class procedure TLog.LogMsg(const AMsg: string);
   begin
      with TLog.Create do
      try
         FMsg := AMsg;
         Notify;
      except
        Free;
        raise;
      end;
    end;

然后我们想像这样发回消息

    procedure THTTPServer.httpServerCommandGet(
      AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo;
      AResponseInfo: TIdHTTPResponseInfo);
    begin
       TLog.LogMsg('test');

这不起作用。

有没有人有一个简单的控制台应用程序工作版本,它是数据模块和控制台上的同步线程?我什至不确定这是否是问题 tbh

我们正在努力理解文档。

Delphi CheckSynchronize

我们简要地看了这段代码,但它对我们不起作用

如何创建一个不终止的控制台应用程序?

console delphi-2007 indy10
1个回答
0
投票

您的

TLog
代码没问题。问题是,默认情况下,控制台应用程序根本没有消息循环,因此没有任何东西运行来自动处理
TIdNotify
请求,就像在 GUI 应用程序中一样。这就是为什么您的代码无法在控制台应用程序中运行的原因。

您的控制台应用程序的主入口点需要定期调用 RTL 的

CheckSynchronize()
函数。您可以使用 RTL 的
SyncEvent
句柄来检测何时有待处理的请求等待处理。

尝试这样的事情:

program MyApp;

{$APPTYPE CONSOLE} 

uses
  ..., Classes, Windows;

...

begin
  ...
  while not SomeCondition do
  begin
    ...
    if WaitForSingleObject(SyncEvent, 100) = WAIT_OBJECT_0 then
      CheckSynchronize;
    ...
  end;
  ...
end.
© www.soinside.com 2019 - 2024. All rights reserved.