Probleme de thread :'(

Résolu
Clercq Messages postés 19 Date d'inscription samedi 18 décembre 2004 Statut Membre Dernière intervention 14 juin 2006 - 8 juin 2006 à 12:00
Clercq Messages postés 19 Date d'inscription samedi 18 décembre 2004 Statut Membre Dernière intervention 14 juin 2006 - 8 juin 2006 à 17:35
Bonjour,

Voila, mon probleme c'est qu'après lancement de ma thread et bien .... elle d'arrete à la deuxieme instruction (visualisation par le debug) et ... je sais pas pourquoi.

Auriez vous une idée sachant que mon code est le suivant :

Unité qui appel le thread :
////////////////////////////////////////////////////////////////////////////////////////////////////////////
unit progEnr;


interface


uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls; //, unit1;


type
  TForm4 = class(TForm)
    GroupBox1: TGroupBox;
    editHeure: TEdit;
    editMinute: TEdit;
    editSeconde: TEdit;
    Button1: TButton;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    GroupBox3: TGroupBox;
    nbH: TEdit;
    nbM: TEdit;
    nbS: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure ShowAsStartupScreen;


  private
    { Déclarations privées }
  public
    { Déclarations publiques }


  end;


var
  Form4 : TForm4;


implementation
uses
    threadEnvar, Unit1;


{$R *.DFM}
procedure TForm4.Button1Click(Sender: TObject);
var
     myThreadEnrCycl : threadEnrCycl;
begin     if (editHeure.text '') or (editMinute.text '')
                              or (editSeconde.text = '')
                              or (nbH.text ='')
                              or (nbM.text ='')
                              or (nbS.text ='') then
        ShowMessage('Veuillez saisir tous leschamps')
     else
     begin
          Form1.debut := editHeure.text+':'+editMinute.text+':'+editSeconde.text;
          Form1.duree := nbH.text+':'+nbM.text+':'+nbS.text;
          //Form1 := TForm1.create;
//          Form1.getEnrConf(heure, minute, seconde, duree);
     end;
     myThreadEnrCycl := threadEnrCycl.Create(False);
//     Form4.Visible := False;
       close;
     if myThreadEnrCycl.Suspended = True then
        messageBox(Handle,'Le thread est terminé','Avertissement',MB_OK);
end;


procedure TForm4.ShowAsStartupScreen;
begin
//  BorderIcons := [];
//  caption := '';
//  BorderStyle := bsDialog;
//  Ok.Visible := false;
  Show;
end;


end.

////////////////////////////////////////////////////////////////////////////////////////////////////////////

Ma thread :

////////////////////////////////////////////////////////////////////////////////////////////////////////////
unit threadEnvar;


interface


uses
  Classes, unit1, Sysutils, pcrInt00, windows;


type
  threadEnrCycl = class(TThread)
  private
    { Déclarations privées }
  protected
    procedure Execute; override;
  end;


implementation
uses
    progEnr;
{ Important : les méthodes et les propriétés des objets dans la VCL ne peuvent
  être utilisées que dans une méthode appelée en utilisant Synchronize, par exemple :


      Synchronize(UpdateCaption);


  où UpdateCaption pourrait être du type :


    procedure threadEnrCycl.UpdateCaption;
    begin
      Form1.Caption := 'Mis à jour dans un thread';
    end; }


{ threadEnrCycl }


procedure threadEnrCycl.Execute;
var
   myThreadEnrCycl : threadEnrCycl;
   saveVar : TextFile;
   ////////////////////
   i : integer;
   resultat : string;
   decimal : integer;
   locator: string;
   address: longint;
   lgth:  longint;
   buffer: PChar;
   mul : integer;
   bufAdd : string;
   tempAdd : string;
   addStr : string;
   sizeStr : integer;
   erreur : integer;
begin
  { Placez le code du thread ici}
     decimal := 0;
     resultat := '';
     addStr := '';
     bufAdd := '';
     tempAdd := '';
     tempAdd := Form1.listAttribTrans[(4 * Form1.cbListVar.ItemIndex) + 1];
     sizeStr := Length(tempAdd);
     if sizeStr < 8 then
     begin
        erreur := 8 - sizeStr;
        for i := 0 to erreur - 1 do
            bufAdd := bufAdd + '0';
     end;
     tempAdd := bufAdd + tempAdd;
     bufAdd := '';
     bufAdd := bufAdd + tempAdd[7];
     bufAdd := bufAdd + tempAdd[8];
     bufAdd := bufAdd + tempAdd[5];
     bufAdd := bufAdd + tempAdd[6];
     bufAdd := bufAdd + tempAdd[3];
     bufAdd := bufAdd + tempAdd[4];
     bufAdd := bufAdd + tempAdd[1];
     bufAdd := bufAdd + tempAdd[2];


     address := StrToInt('0x' + bufAdd);
     locator := [mailto:'@canup_1_00A' '@canup_1_00A'];                    //'@TCU_2';
     if Form1.listAttribTrans[4*Form1.cbListVar.ItemIndex + 2] = 'NULL' then
        lgth := StrToInt(Form1.listAttribTrans[4*Form1.cbListVar.ItemIndex + 3])-1//4;//20;
     else
         lgth := StrToInt(Form1.listAttribTrans[4*Form1.cbListVar.ItemIndex + 3]);


          buffer := StrAlloc(20);


     AssignFile(saveVar, Form1.nameFile);
     ReWrite(saveVar); //Ouverture en mode ecriture     while not (Time StrToTime(Form1.debut)) and (arretEnr 0) do
     begin
     end;     while not (Time (StrToTime(Form1.debut) + StrToTime(Form1.duree)) ) and (arretEnr 0) do
     begin
          PCR_ReadMemoryArea(locator,
                             address,
                             lgth,
                             buffer);
          if Form1.listAttribTrans[4*Form1.cbListVar.ItemIndex + 2] = 'NULL' then
          begin
               for i := 0 to (lgth - 1) do
               begin
                    if i = 0 then
                       mul := 1
                    else
                        mul := i*256;
                    decimal := decimal + integer(buffer[i])*mul;
               end;
          end
          else
          begin
               for i := (lgth - 1) downto 0 do
               begin
                    if i = (lgth - 1) then
                       mul := 1
                    else
                        mul := i*256;
                    decimal := decimal + integer(buffer[i])*mul;
               end;
          end;
          WriteLn(saveVar, IntToStr(decimal));
      end;
      closeFile(saveVar);
end;


end.
////////////////////////////////////////////////////////////////////////////////////////////////////////////

Je sais que mettre le code comme ça ne rend pas le post conci et symaptique mais au moins vous pouvez voir toutes mes erreurs :p

Merci à vous d'avance.

Tcho et à bientot :D

2 réponses

roud59 Messages postés 99 Date d'inscription jeudi 16 septembre 2004 Statut Membre Dernière intervention 26 juillet 2007 2
8 juin 2006 à 14:34
Salut

Sans avoir tout lu et tout compris, je remarque que tu écris, dans ton Button1Click :

 myThreadEnrCycl := threadEnrCycl.Create(False); OK ça lance le thread
...
 Close; Si Form4 est la fenêtre principale, l'instruction  ferme l'application entraînant l'arret du thread.

Peut être que l'erreur n'est pas là, mais je te donne une piste possible.

En plus
- dans Execute tu devrais lire la propriéte terminated et quitter la methode quand cette proriété vaut vrai. Rajoute dans tes while : (and not terminated)
- normalement tu devrais détuire manuellement le thread à la fin du programme (ou quand il est devenu inutile) et tu ne peux pas le faire car tu déclares myThreadEnrCycl comme variable locale.

Bon courage

Roud59
3
Clercq Messages postés 19 Date d'inscription samedi 18 décembre 2004 Statut Membre Dernière intervention 14 juin 2006
8 juin 2006 à 17:35
Merci infiniment, maintenant ça fonctionne, j'ai mis un Visible := False; et j'ai rajouter le and not terminated :D

Bonne continuation à toi et encore merci.

Clercq.
0
Rejoignez-nous