Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   TIdNotify funktion (https://www.delphipraxis.net/153536-tidnotify-funktion.html)

totti14 6. Aug 2010 19:41

Delphi-Version: 2010

TIdNotify funktion
 
Hallöchen,
dammit ich aus verschieden Threads in mein Log Memo schreiben kann habe ich mir folgende Procedure mit TIdNotify gebastelt. Ich hatte es vorher mit TIdSync, aber da wird ja ein Päuschen eingelegt, bis es abgearbeitet ist ?

Delphi-Quellcode:
Type
TVarRecArray = array of TVarRec;

Log = class(TIdNotify)
    protected
      fMemo: TMemo;
      fStr: AnsiString;
      fArgs: TVarRecArray;
      procedure DoNotify; override;
    public
      constructor Create(const Str: AnsiString; const Args: array of TVarRec); ReIntroduce; overload;
      class procedure cs_Log(const Str: AnsiString; const Args: array of TVarRec);
  end;
implementation
//----------------------------
function CopyArgs(a: array of TVarRec): TVarRecArray;
var
  i: Integer;
begin
  SetLength(Result, length(a));
  try
    for i := 0 to length(a) - 1 do
      Result[i] := a[i];
  except
  end;
end;
// ----------------------
constructor Log.Create(const Str: AnsiString; const Args: array of TVarRec);
begin
  inherited Create;
  fMemo := Frm.ServerInfo;
  fStr := Str;
  fArgs := CopyArgs(Args);
end;
// ---------------------------------------------------------------------------------
class procedure Log.cs_Log(const Str: AnsiString; const Args: array of TVarRec);
begin
  with Create(Str, Args) do
    Notify;
end;
// --------------------
procedure Log.DoNotify;
Var
  Res, Res2: AnsiString;
  ADate: TDateTime;
begin
  inherited;
  begin
    ADate := now;
    try
      Res := Format(fStr, fArgs);
    except
    end;
    Res2 := FormatDateTime('c', ADate);
    Res2 := Res2 + ' ' + Res;
    fMemo.Lines.Add(Res2);
  end;
end;

Main:
  Log.cs_Log('Client.Connect %8s/%.4X', [PeerIP,PeerPort]);
Die Frage ist jetzt: cs_log gibt sich, nach absenden der Message, selbst wieder frei ?
Funktionieren tut es so prima aber ist das auch richtig so? Gerade wegen der Args.

Besten Dank
totti14

totti14 7. Aug 2010 23:39

AW: TIdNotify funktion
 
Hi,
wenn ich nochmal nachfragen dürfte:

Hat das noch niemand so benutzt oder macht man das ganz anders ?

Danke
totti14


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:36 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz