🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.misc
Article #735

Thread and Tsession

#735
From: Georges
Date: Fri, 26 Sep 1997 00:00
159 lines
3983 bytes
unit UTThreadOID2DB;

I will write a thread who append a record in a table
For that, i create a session and a database but when i destroy them,
all access to my database in Main Process failed!

any idea?

Best Regards



Emmanuel



interface

uses
  Classes, Grids, dbtables, Db;

type
  TThreadOID2DB = class(TThread)
  private
    { D�clarations priv�es }
    TSG:      TStringGrid;
    S:        TSession;
    DB:       TDatabase;
    Table:    TTable;
    SN:       String;
    IP:       String;
    F:        AnsiString;
    Date:     TDateTime;
    Heure:    TDateTime;
    Duree:    Integer;
  public
    constructor Create( T: TStringGrid; AddIP: String; Formule:
AnsiString;
                        D,H: TDateTime; Interv: Integer); virtual;
  protected
    procedure Execute; override;
  end;

implementation

uses Unit1,SysUtils, logging;

{ Important : Les m�thodes et propri�t�s des objets des VCL ne peuvent
�tre
  utilis�es que dans des m�thodes appel�es avec Synchronize; par
exemple :

      Synchronize(UpdateCaption);

  et UpdateCaption pourrait �tre :

    procedure TThreadOID2DB.UpdateCaption;
    begin
      Form1.Caption := 'Mise � jour dans un thread';
    end; }

{ TThreadOID2DB }

constructor TThreadOID2DB.Create( T: TStringGrid; AddIP: String;
Formule: AnsiString;
                                  D,H: TDateTime; Interv: Integer);
var
   i,j: Integer;
begin
  IP:=AddIP;
  F:=Formule;
  Date:=D;
  Heure:=H;
  Duree:= Interv;

  SN:='Session'+IntToStr(random(32767));
  While Sessions.FindSession(SN)<>nil do
  begin
       SN:='Session'+IntToStr(random(32767));
  end;

  S:=Sessions.OpenSession(SN);
  if S=nil then
     exit;

  TSG:=TStringGrid.Create(MainForm);
  TSG.RowCount:=T.RowCount;
  TSG.ColCount:=T.ColCount;
  For i:=0 to T.ColCount-1 do
      For j:=0 to T.RowCount-1 do
          TSG.Cells[i,j]:=T.Cells[i,j];

  inherited create(false);

  Priority:=tpLower;
end;

procedure TThreadOID2DB.Execute;
var
   C,R: Integer;
begin
  { Placez le code du thread ici }
  Db:=TDatabase.Create(nil);
  with Db do
  begin
       Connected:=false;
       AliasName:='SnmpManagerMDB';
       DatabaseName:='Database'+SN;
       SessionName:=SN;
       LoginPrompt:=False;
       Params.Add('USER NAME=Admin');
       Connected:=true;
  end;

  Table:=TTable.Create(nil);
  With Table do
  begin
       DatabaseName:='Database'+SN;
       SessionName:=SN;
       TableName:='Valeurs OID ou Formule';
       Active:=true;
  end;

  // Enregistrement des donn�es
  try
     For C:=0 to TSG.ColCount-1 do
         For R:=1 to TSG.RowCount-1 do
         With Table do
            begin
                 Log('Append');
                 Append;
                 FieldValues['AdresseIP']:=IP;
                 if TSG.Cells[C,0]='' then
                    FieldValues['OID']:=F
                 else
                    FieldValues['OID']:=TSG.Cells[C,0];
                 FieldValues['Valeur']:=TSG.Cells[C,R];
                 FieldValues['Time']:=Heure;
                 FieldValues['Date']:=Date;
                 FieldValues['Dur�e']:=Duree;
                 Post;
                 Log('Post');
            end;
     Log('Fin ');
  except
     On Ex: EDBEngineError do
         Log('TThreadOID2DB=>EDBEngineError: '+Ex.Message);
  end;

  Table.Active:=false;
  Table.Destroy;  

  Db.Connected:=False;
  Db.Destroy;
  S.Destroy;
end;

end.
Technical Details
Message-ID: <342b54c5.87983977@news.sct.fr>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10a726,a0253b27330f3d9f,start
X-Google-Attributes: gid10a726,public
X-Google-Thread: 10baee,a0253b27330f3d9f,start
X-Google-Attributes: gid10baee,public
From: eai@nordnet.fr (Georges)
Subject: Thread and Tsession
Date: 1997/09/26
Message-ID: <342b54c5.87983977@news.sct.fr>
X-Deja-AN: 275649284
Newsgroups: comp.lang.pascal.delphi.misc,fr.comp.lang.pascal