Problem jest taki: gdy damy Sleepa w Execute, to podczas zwalniania trzeba poczekać aż się obudzi.
http://www.delphi3000.com/articles/article_2630.asp
Na podstawie tego art'a zaimplementowałem to do swojej aplikacji, która ma 3 wątki. Jednakże przy zamknięciu jest losowy poślizg (ok. 10s). Czy da się to obejść? Gdy testowałem przy jednym wątku to poślizgu nie ma (przy dwóch to raz na jakąś próbę).
unit ExThread;
interface
uses
Classes, Windows, Sysutils;
type
TExThread = class(TThread)
private
{ Private declarations }
hEvent: THandle;
procedure Sleep(Milliseconds:Integer);
protected
procedure Execute; override;
public
constructor Create(Suspended:Boolean);
destructor Destroy; override;
end;
implementation
constructor TExThread.Create(Suspended: BOolean);
var
lpEventAttributes: PSecurityAttributes;
begin
inherited;
FillChar(lpEventAttributes,Sizeof(PSecurityAttributes),0);
hEvent := CreateEvent(lpEventAttributes,false,False,'EXIT_EVENT');
if hEvent = 0 then raise Exception.Create('Unable to create EXIT_EVENT');
end;
destructor TExThread.Destroy;
begin
if not Terminated then Terminate;
SetEvent(hEvent);
CloseHandle(hEvent);
inherited;
end;
procedure TExThread.Execute;
begin
while not terminated do begin
{ Place thread code here }
Sleep(15000)
end;
end;
procedure TExThread.Sleep(Milliseconds:Integer);
begin
WaitForSingleObject(hEvent,Milliseconds);
end;
end.