Delphi - Waitable Timer

Delphi - Waitable Timer

Postby DarkByte » 06 Jan 2010, 19:59

Un waitable timer blocheaza executia thread-ului in care e folosit pentru o durata specificata de timp.

Pentru a-l folosi in Delphi, aveti mai jos un exemplu de cod:

  1. procedure WaitableTimer;
  2. const sec = 10000000;
  3. var wTimer, lBusy : Longint;
  4.     interval : LARGE_INTEGER;
  5. begin
  6.   wTimer := CreateWaitableTimer(nil, True, PANSIChar('myTimer' + IntToStr(GetTickCount)));
  7.   {numele poate fi si gol (empty string}
  8.  
  9.   interval.QuadPart :=  - sec * 3; {numarul de secunde}
  10.                                    {valoare negativa = valoare relativa}
  11.                                    {valoare pozitiva = valoare absoluta}
  12.  
  13.   Repeat
  14.     SetWaitableTimer(wTimer, TLargeInteger(interval), 0, nil, nil, false);
  15.     lBusy := WaitForSingleObject(wTimer, INFINITE);
  16.     If lBusy = WAIT_OBJECT_0 Then
  17.       Begin
  18.         Execute_Something; {aici se pune codul care se doreste executat la fiecare X secunde}
  19.       End;
  20.   Until some_condition;
  21.  
  22.   CancelWaitableTimer(wTimer);
  23.  
  24.   CloseHandle(wTimer);
  25. end;
0,0p / 0 votes
User avatar
DarkByte
11011011
 
Joined: 29 Dec 2009
Status: 136

Re: Waitable Timer in Delphi

Postby v0id » 07 Jan 2010, 11:07

... si daca vreti sa scoateti sistemul din starea de Standby/Sleep/Hibernate la expirarea timer-ului, apelati functia SetWaitableTimer cu ultimul parametru True:
  1. SetWaitableTimer(wTimer, TLargeInteger(interval), 0, nil, nil, true);


Pentru mai multe informatii: SetWaitableTimer on MSDN.
1p / 1 votes
A good coder is never on holiday - he may be working on a different machine, that's about as far as it gets.
User avatar
v0id
Word
 
Joined: 05 Jan 2010
Location: 127.0.0.1
Status: 39.5


Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests