Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
Delphi 10 Seattle Enterprise
|
AW: Thread: Suspend und Resume deprecated
5. Feb 2012, 10:41
Klar geht das eleganter:
Delphi-Quellcode:
type
TMyThread = class(TThread)
private
FEvent: THandle;
...
public
destructor Destroy; override;
procedure Terminate; override;
procedure SuspendWork;
procedure ContinueWork;
...
end;
procedure TMyThread.Terminate;
begin
inherited;
SetEvent( FEvent );
end;
destructor TMyThread.Destroy;
begin
if not Terminated then
begin
Terminate;
WaitFor;
end;
inherited;
end;
constructor TMyThread.Create;
begin
(* Signaled-Event anlegen *)
FEvent := CreateEvent(nil, true, true, '');
...
end;
procedure TMyThread.ContinueWork;
begin
(* Event auf Signaled setzen *)
SetEvent(FEvent);
end;
procedure TMyThread.Execute;
begin
while not Terminated do
begin
(* Falls Event Non-Signaled ist, warten *)
if
( WaitForSingleObject( FEvent, INFINITE ) = WAIT_OBJECT_0 ) and
not Terminated
then
begin
// Hier jetzt das rein, was der Thread abarbeiten soll
end;
end;
end;
procedure TMyThread.SuspendWork;
begin
(* Event auf Non-Signaled setzen *)
ResetEvent(FEvent);
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
|
|
Zitat
|