Delphi 2007 Updating a console application from a datamodule using TIdHTTPServer within httpServerCommandGet

huangapple go评论53阅读模式
英文:

Delphi 2007 Updating a console application from a datamodule using TIdHTTPServer within httpServerCommandGet

问题

以下是翻译好的内容:

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

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

procedure TLog.DoNotify;
begin
  writeln(FMsg); //这可能完全错误
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');
end;

这不起作用。

有没有人有一个简单的工作版本的控制台应用程序,可以在数据模块和控制台上同步线程?老实说,我甚至不确定是否这是问题。

我们在试图理解文档方面遇到了困难。

Delphi CheckSynchronize

我们曾简要查看过这段代码,但对我们来说没有用。

https://stackoverflow.com/questions/2591774/how-to-create-a-console-application-that-does-not-terminate


<details>
<summary>英文:</summary>

We have the following class we want to use to update the console from within a procedure on a datamodule using 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;


We want to then send messages back like this
 
procedure THTTPServer.httpServerCommandGet(
  AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo;
  AResponseInfo: TIdHTTPResponseInfo);
begin
   TLog.LogMsg(&#39;test&#39;);
 

This is not working.

Does anyone have a simple working version of a console app which is synchronized threads on the datamodule and the console? I&#39;m not even sure if that is the problem tbh

We are struggling to make sense of the documentation. 

[Delphi CheckSynchronize](https://i.stack.imgur.com/LmTAI.png)


 
We had a brief look at this code, but it didn&#39;t work for us

https://stackoverflow.com/questions/2591774/how-to-create-a-console-application-that-does-not-terminate


</details>


# 答案1
**得分**: 0

你的 `TLog` 代码没问题。问题在于,默认情况下,控制台应用程序根本没有消息循环,因此没有运行以自动处理 `TIdNotify` 请求,就像在 GUI 应用程序中一样。这就是为什么你的代码在控制台应用程序中不起作用的原因。

你的控制台应用程序的主入口点需要定期调用 RTL 的 `CheckSynchronize()` 函数。你可以使用 RTL 的 `SyncEvent` 句柄来检测是否有待处理的请求等待处理。

尝试像这样做:

```delphi
program MyApp;

{$APPTYPE CONSOLE} 

uses
  ..., Classes, Windows;

...

begin
  ...
  while not SomeCondition do
  begin
    ...
    if WaitForSingleObject(SyncEvent, 100) = WAIT_OBJECT_0 then
      CheckSynchronize;
    ...
  end;
  ...
end.
英文:

Your TLog code is fine. The problem is, by default, a console application simply does not have a message loop, so there is nothing running to process the TIdNotify requests automatically, like in a GUI app. That is why your code is not working in a console app.

Your console app's main entry point needs to call the RTL's CheckSynchronize() function periodically. You can use the RTL's SyncEvent handle to detect when there are pending requests waiting to be processed.

Try something like this:

program MyApp;

{$APPTYPE CONSOLE} 

uses
  ..., Classes, Windows;

...

begin
  ...
  while not SomeCondition do
  begin
    ...
    if WaitForSingleObject(SyncEvent, 100) = WAIT_OBJECT_0 then
      CheckSynchronize;
    ...
  end;
  ...
end.

huangapple
  • 本文由 发表于 2023年3月7日 10:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定