Registriert seit: 10. Apr 2006
Ort: Leverkusen
970 Beiträge
Delphi 6 Professional
|
AW: Default-Wert aus Funktion nach timeout (D6)
31. Jul 2019, 14:26
Hmm..
kleine Korrectur für D6
Delphi-Quellcode:
type
TWorkthread = class(TTHread)
private // Delphi 6 kennt noch kein strict
FValue: Integer;
protected
procedure Execute; override;
public
property Value: Integer read FValue;
end;
procedure TWorkthread.Execute;
begin
// code to get the data goes here
// for now simulate a 10 second delay
sleep(10000);
FValue := 666;
end;
function get_value: integer;
const
OneSecond = 1000;
DefaultValue = 999;
var
LResult: dword;
LWorker : TWorkthread;
begin
// thread starts running immediately
LWorker := TWorkthread.Create(False); // TThread benötigt (False) bei D6
try
LResult := WaitForSingleObject(LWorker.Handle, OneSecond);
if LResult = WAIT_OBJECT_0 then
Result := LWorker.Value
else if LResult = WAIT_TIMEOUT then begin
LWorker.FreeOnTerminate := true;
LWorker := nil;
//abandon the thread
Result := DefaultValue;
end
else
raise Exception.CreateFmt(' Thread creation failed, wait result was $%x',[LResult]);
finally
LWorker.Free;
end;
end;
(Ja ich Verwende Delphi 6 Pro und will NICHT wechseln!)
|