IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Problème de création de thread


Sujet :

Langage Delphi

  1. #1
    Membre actif Avatar de [Silk]
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2005
    Messages
    198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Mai 2005
    Messages : 198
    Points : 201
    Points
    201
    Par défaut Problème de création de thread
    Salut,

    En ce moment je suis sur un scanner de port en multithreads et j'ai 5 thread qui sont chargés de scanner une plage donnée de ports, mais le probleme c'est que j'ai une acces violation lors de la création du 1er ou 2eme thread (?? delphi me point sur la création du deuxième thread)
    J'ai pourtant bien mis des sections critiques et des appels à Synchronize dans mes threads. à tout les coups j'ai du me tromper sur un truc tout bête...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
     
    procedure TMainForm.ScanClick(Sender: TObject);
    var
    i,i3 : integer;
    begin
     
     Entier := '0';
     
     Try
      PDebut := StrToInt(edDebut.Text);
      PFin :=   StrToInt(edFin.Text);
     except
      MessageDlg('Les ports ne sont pas corrects',mtError,[mbOk],0);
      Exit;
     end;
     
     If PFin < PDebut then
      begin
       MessageDlg('Le port de fin d''intervalle ne peut pas être plus grand que celui de début',mtError,[mbOk],0);
       Exit;
      end;
     
     If not Scanning then
      begin
        Scan.Caption := 'Arrêter';
        Scanning := true;
        bar.Max:=StrToInt(edFin.Text) - StrToInt(edDebut.Text);
        portlist.Clear;
        LBPortsOpen.Items.Clear;
        stat.Caption:='En cours...';
     
        NbPorts := PFin-PDebut;
        PortsTemp := (NbPorts/5);
        StrTemp := FloatToStr(PortsTemp);
        ShowMessage(StrTemp);
     
        TCP1.Host := edIP.Text;
        TCP2.Host := edIP.Text;
        TCP3.Host := edIP.Text;
        TCP4.Host := edIP.Text;
        TCP5.Host := edIP.Text;
     
        For i := 0 to length(StrTemp) do begin
          If StrTemp[i] = ',' then begin
             {For i2 := i+1 to length(StrTemp) do
                Virgule := Virgule + StrTemp[i2]; }
             For i3 := 0 to i-1 do
                Entier := Entier + StrTemp[i3];
          end else
            Entier := Entier +StrTemp[i];
        end;
     
        Ports1 := StrToInt(Entier);
        Ports2 := StrToInt(Entier);  
        Ports3 := StrToInt(Entier);
        Ports4 := StrToInt(Entier);
        Ports5 := NBPorts-Ports1-Ports2-Ports3-Ports4;
     
        Try
          Thread1.Create(False);
          Thread2.Create(False);    >>> Erreur sur cette ligne
          Thread3.Create(False);
          Thread4.Create(False);
          Thread5.Create(False);
        except
           MessageDlg('Erreur lors de la création des threads. Opération annulée',mtError,[mbOk],0);
           Scan.Caption := 'Scan';
           Scanning := False;
           Stat.Caption := 'Erreur de threads';
           Exit;
        End;
      end else
      begin
        Scan.Enabled := false;
        Scan.Caption := 'Scan';
        Scanning := false;
        stat.Caption:='Arrêt en cours...';
     
        //Arrêt du Thread1
        repeat
        If Finished1 then begin
                            Thread1.Terminate;
                          end;
        Until Finished1;
     
        //Arrêt du Thread2
        repeat
        If Finished2 then begin
                            Thread2.Terminate;
                          end;
        Until Finished2;
     
        //Arrêt du Thread3
        repeat
        If Finished3 then begin
                            Thread3.Terminate;
                          end;
        Until Finished3;
     
        //Arrêt du Thread4
        repeat
        If Finished4 then begin
                            Thread4.Terminate;
                          end;
        Until Finished4;
     
        //Arrêt du Thread5
        repeat
        If Finished5 then begin
                            Thread5.Terminate;
                          end;
        Until Finished5;
     
        Stat.Caption := 'Arrêté.';
        Scan.Enabled := true;
      end;
    Voici le code d'un des threads :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
     
    Procedure TThread1.UpdateBar;
    begin
    MainForm.bar.Position := MainForm.Bar.Position + 1;
    end;
     
     
    procedure TThread1.Execute;
    var
    i,j : integer;
    begin
        FreeOnTerminate := true;
     
        ListPorts1.Clear;
        ListOpen1.Clear;
     
        If Ports1 <=0 then Begin Finished1 := true; Exit; End;
        Finished1 := false;
     
        For i:= PDebut to PDebut+Ports1 do
        begin
          listPorts1.add(IntToStr(i));
          If not Scanning then break;
           Try
             Synchronize(Thread1,UpdateBar);
             MainForm.TCP1.Port := i;
             MainForm.TCP1.Connect(100);
             listOpen1.Add('Ouvert');
           Except
             listOpen1.Add('Fermé');
           end;
          MainForm.TCP1.disconnect
           end;
        Finished1 := true;
     
        If Scanning then
        begin
          CriticalSection.Enter;
          For j := 0 to ListPorts1.Count-1 do
          begin
            If not Scanning then Break;
            MainForm.Portlist.Items.Add.Caption := ListPorts1[j];
            MainForm.Portlist.Items.Item[MainForm.Portlist.Items.Count - 1].SubItems.Add(ListOpen1[j]);
            MainForm.Portlist.ItemIndex := MainForm.Portlist.Items.Count - 1;
          end;
          CriticalSection.Leave;
        end;
    end;

  2. #2
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Bonjour,

    Il y 2 choses qui me genent dans le code :

    1) l'instruction "Thread1.create(false)" : j'aurais écrit "Thread1:=TThread.create(false)" ou "Thread1:=TThread1.create(false)".

    2) la présence contracdictoire des 2 instructions "FreeOnTerminate:=true" dans TThread1.execute et "Thread1.Terminate" dans le thread principal.

  3. #3
    Membre actif Avatar de [Silk]
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2005
    Messages
    198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Mai 2005
    Messages : 198
    Points : 201
    Points
    201
    Par défaut
    Pour le 2) c'est normal (du moin je crois ?)c'est voulu quand le thread se termine je le libère.
    Pour le 1 c'est la solution à mon problème, à chaque fois je me fais avoir par ce truc de constructeur je suis honteux

    merci pour ta réponse Graffito

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Ant] [Ejb] Probléme de création d'ejb avec Genic
    Par rivierem dans le forum JOnAS
    Réponses: 7
    Dernier message: 25/06/2004, 16h21
  2. Réponses: 7
    Dernier message: 16/06/2004, 15h02
  3. Problème de création de table sous MySql
    Par ducamba dans le forum Requêtes
    Réponses: 2
    Dernier message: 21/06/2003, 09h59
  4. Problème de création de fenêtre
    Par tomateauketchup dans le forum DirectX
    Réponses: 1
    Dernier message: 08/06/2003, 19h42
  5. [Rave Report] problème de création dynamique
    Par Nivux dans le forum Rave
    Réponses: 2
    Dernier message: 24/05/2003, 00h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo