(Gast)
n/a Beiträge
|
AW: Timer Reset ?
24. Feb 2017, 17:20
Man kann sich auch selbst einen Timer machen mit
WaitableTimer
Delphi-Quellcode:
procedure TForm1.ButtonClick(Sender: TObject);
var
AlarmTimer : THandle;
Zeit : Large_Integer;
start, ende : Cardinal;
begin
Memo1.lines.add('SetWaitableTimer: 1 sec Running');
start:= GetTickCount;
AlarmTimer := CreateWaitableTimer(nil, False, nil);
CancelWaitableTimer(AlarmTimer); // goto Start
Zeit.QuadPart := 1 * (-10000000); // 1 Sekunde
SetWaitableTimer(AlarmTimer, Zeit.Quadpart, 0, nil, nil, False);
while WaitForSingleObject(AlarmTimer, 20) <> Wait_Object_0 do // Beispiel: 20msec
begin Application.ProcessMessages; end;
end1:= GetTickCount;
Memo1.lines.add(INTTOSTR(end1-start)+' msec');
Memo1.lines.add('WaitableTimer: Ready');
end;
AUSGABE:
SetWaitableTimer: 1 sec Running
1000 msec
WaitableTimer: Ready
Alternative: 10 sec
Delphi-Quellcode:
var
Form1: TForm1;
start, end1 : Cardinal;
procedure TimerAPCProc (param: Pointer; dwTimerLowValue, dwTimerHighValue: CARDINAL); stdcall;
begin
end1:= GetTickCount;
Form1.Memo1.lines.add(INTTOSTR(end1-start)+' msec');
Form1.Memo1.lines.add('WaitableTimer: Ready');
end;
procedure TForm1.Button9Click(Sender: TObject);
var
HTimer: THandle;
DueTime: Int64;
begin
Memo1.lines.add('SetWaitableTimer: 10 sec Running');
start:= GetTickCount;
HTimer:= CreateWaitableTimer (nil, False, nil);
if HTimer <> 0 then
begin
DueTime:= 10*(-10000000);//10 sec
if SetWaitableTimer (HTimer, DueTime, 0,//once
@TimerAPCProc,
Pointer (HTimer), False) then
BEGIN SleepEx (INFINITE, True); END;
CloseHandle (HTimer);
end;
end;
Geändert von t.roller (24. Feb 2017 um 21:22 Uhr)
|
|
Zitat
|