Problème avec TList et Threads
Bonjour tous le monde,
J'ai un petit soucis j'essaye de faire un programme qui teste une liste d'ips qu'on lui charge depuis un fichier texte pour qu'il fasse differents tests dessus.
Pour avoir une gestion dynamique des threads et pour que l'utilisateur puisse choisir sur combien de threads testes etc ... j'ai utilise une TList mais j'ai un soucis lors de la creation du thread une exception est soulevee.
Unit1.pas
Code:
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
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
ListBox1: TListBox;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Unit2;
Type
PTestThread = ^TTestThread;
Var
IpToTest : TStringList;
ThreadList : TList;
TestThread : PTestThread;
MaxThreads : Integer;
i : Integer;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
i := 0;
while i < IpToTest.count do
begin
if ThreadList.count <= MaxThreads then
begin
new(TestThread);
TestThread^.create(true);
TestThread^.FreeOnTerminate := true;
TestThread^.resume;
ThreadList.add(TestThread);
inc(i);
end;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
openDialog : TOpenDialog;
begin
openDialog := TOpenDialog.Create(self);
try
openDialog.InitialDir := GetCurrentDir;
openDialog.Options := [ofFileMustExist];
openDialog.Filter := 'Tous les fichiers|*.*';
openDialog.FilterIndex := 2;
if openDialog.Execute then begin
IpToTest.LoadFromFile(openDialog.FileName);
ListBox1.Items.LoadFromFile(openDialog.FileName);
end;
finally
openDialog.Free;
end;
end;
Initialization
IpToTest := TStringList.Create;
ThreadList := TList.Create;
Finalization
IpToTest.Free;
ThreadList.Free;
end. |
Unit2.pas
Code:
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
| unit Unit2;
interface
uses
Classes;
type
TTestThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TTestThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TTestThread }
procedure TTestThread.Execute;
begin
{ Place thread code here }
end;
end. |
Je suis sur (quasi) que le probleme vient des pointeurs, si qqn peut m'eclairer je lui en serait tres reconnaissant, merci
P.S : desole pour la poncutatiom j'utilise un clavier qwerty