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;