Ho un'applicazione Delphi che genera 6 thread anonimi su alcuni eventi TTimer.OnTimer.Come terminare i thread anonimi in Delphi alla chiusura dell'applicazione?
Se chiudo l'applicazione dal pulsante X nella barra del titolo Violazione di accesso all'indirizzo $ C0000005 viene generato e i report FastMM hanno perso oggetti TAnonymousThread.
Qual è il modo migliore per liberare thread anonimi in Delphi creati all'interno dell'evento OnTimer con il metodo TThread.CreateAnonymousThread()?
soluzione che ha funzionato per me:
Creato un wrapper dei fili anonimi che li terminano sull'essere Free-ed.
type
TAnonumousThreadPool = class sealed(TObject)
strict private
FThreadList: TThreadList;
procedure TerminateRunningThreads;
procedure AnonumousThreadTerminate(Sender: TObject);
public
destructor Destroy; override; final;
procedure Start(const Procs: array of TProc);
end;
{ TAnonumousThreadPool }
procedure TAnonumousThreadPool.Start(const Procs: array of TProc);
var
T: TThread;
n: Integer;
begin
TerminateRunningThreads;
FThreadList := TThreadList.Create;
FThreadList.Duplicates := TDuplicates.dupError;
for n := Low(Procs) to High(Procs) do
begin
T := TThread.CreateAnonymousThread(Procs[n]);
TThread.NameThreadForDebugging(AnsiString('Test thread N:' + IntToStr(n) + ' TID:'), T.ThreadID);
T.OnTerminate := AnonumousThreadTerminate;
T.FreeOnTerminate := true;
FThreadList.LockList;
try
FThreadList.Add(T);
finally
FThreadList.UnlockList;
end;
T.Start;
end;
end;
procedure TAnonumousThreadPool.AnonumousThreadTerminate(Sender: TObject);
begin
FThreadList.LockList;
try
FThreadList.Remove((Sender as TThread));
finally
FThreadList.UnlockList;
end;
end;
procedure TAnonumousThreadPool.TerminateRunningThreads;
var
L: TList;
T: TThread;
begin
if not Assigned(FThreadList) then
Exit;
L := FThreadList.LockList;
try
while L.Count > 0 do
begin
T := TThread(L[0]);
T.OnTerminate := nil;
L.Remove(L[0]);
T.FreeOnTerminate := False;
T.Terminate;
T.Free;
end;
finally
FThreadList.UnlockList;
end;
FThreadList.Free;
end;
destructor TAnonumousThreadPool.Destroy;
begin
TerminateRunningThreads;
inherited;
end;
Fine ecco come si può chiamare:
procedure TForm1.Button1Click(Sender: TObject);
begin
FAnonymousThreadPool.Start([ // array of procedures to execute
procedure{anonymous1}()
var
Http: THttpClient;
begin
Http := THttpClient.Create;
try
Http.CancelledCallback := function: Boolean
begin
Result := TThread.CurrentThread.CheckTerminated;
end;
Http.GetFile('http://mtgstudio.com/Screenshots/shot1.png', 'c:\1.jpg');
finally
Http.Free;
end;
end,
procedure{anonymous2}()
var
Http: THttpClient;
begin
Http := THttpClient.Create;
try
Http.CancelledCallback := function: Boolean
begin
Result := TThread.CurrentThread.CheckTerminated;
end;
Http.GetFile('http://mtgstudio.com/Screenshots/shot2.png', 'c:\2.jpg');
finally
Http.Free;
end;
end
]);
end;
Nessuna perdita di memoria, una corretta chiusura e facile da usare.
"thread anonimi" - Oh grandioso .. cosa ci ha imposto Embarcadero ora? –
@ Martin: Niente di spaventoso, davvero. È un thread il cui comportamento viene fornito al momento della creazione da un metodo anonimo. Ti consente di utilizzare chiusure durante la definizione dei thread. –
È continuamente creare/distruggere discussioni, qualcosa che ho passato negli ultimi 20 anni a dire agli sviluppatori di evitare. Tuttavia, se non è effettivamente iniziato, non riesco a capire perché dovrebbe esserci un AV. –