consoledelphi-2007indy10

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


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('test');

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'm not even sure if that is the problem tbh

We are struggling to make sense of the documentation.

Delphi CheckSynchronize

We had a brief look at this code, but it didn't work for us

How to create a console application that does not terminate?


Solution

  • 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.