J'utilise Delphi 2007 et les threads.Créer un événement et partager des variables
Mon problème (désolé, je vais essayer d'expliquer mieux):
1) Je crée un fichier "utilities.pas" où j'ai la fonction i utiliser plus. 2) J'ai créé un nouveau programme, dans ce programme j'ai un thread 3) dans la méthode d'exécution du thread j'appelle une fonction dans mon fichier "utilities.pas". cette fonction se connecte à un ftp en utilisant des composants intelligents (tclftp). Ce composant enregistre le serveur responce dans un événement dédié. Ce que je voudrais faire est de sauvegarder le journal dans une liste de chaînes, puis de renvoyer la liste de chaînes au thread appelant.
Cela fait partie du fichier "utilities.pas":
// I created TEventHandlers because it's the only way to assign the event runtime
// without having a class
type
TEventHandlers = class
procedure clFtp1SendCommand(Sender: TObject; const AText: string);
end;
var EvHandler: TEventHandlers;
// this is the porcedure called from the thread. i want to send the stringlist
// back to it containing the ftp log
procedure Test(VAR slMain: tStringlist);
var cFTP: TclFtp;
begin
cFTP := TclFtp.Create(nil);
cFTP.Server := 'XXX';
cFTP.UserName := 'XXX';
cFTP.Password := 'XXX';
cFTP.OnSendCommand := EvHandler.clFtp1SendCommand;
// i connect to the ftp
cFTP.Open;
FreeAndNil(cFTP);
end;
procedure TEventHandlers.clFtp1SendCommand(Sender: TObject; const AText: string);
begin
// here the component (cftp) sends me back the answer from the server.
// i am logging it
// HERE IT'S THE PROBLEM:
// I can't reach slMain from here.....
slmain.add(Atext);
end;
c'est le fil d'appel:
procedure TCalcThread.Execute;
var slMain: tstringlist;
begin
inherited;
slmain := tstringlist.create(nil);
Test(slmain);
if slMain.count > 0 then
slMain.savetofile('c:\a.txt');
// i won't free the list box now, but in the thread terminated.
end;
c'est le programme principal:
procedure TfMain.ThreadTerminated(Sender: TObject);
Var ExThread: TCalcThread;
begin
ExThread := (Sender as TCalcThread);
if ExThread.slMain.Count > 0 then
ExThread.slMain.SaveToFile('LOG\Errori.log');
freeandnil(slMain);
end;
S'il vous plaît quelqu'un peut-il m'aider à résoudre ce problème? Je ne sais vraiment pas quoi faire. J'espère que maintenant c'est plus clair.
p.s. merci pour toute la réponse ..
Typo: SLmail doit être: slMain –
Je suppose que vous devez faire quelque chose avec slMain après que vous appelez cFTP.Open? Si oui, vous devriez ajouter un commentaire. Sinon, comme écrit, slMain n'est pas pertinent. –
La procédure "Test" est-elle exécutée dans un thread? Si oui, vous devriez le préciser. –