Reprinted from: In Case of blog
This is a special study of the second parameter of the SetWaitableTimer (starting time)
It has a positive, negative, 0 value of three cases, preceded by 0 values
Learn negative values first (relative time), that is, how long it takes to start from the current count
This relative time is measured in 1/100 nanoseconds, such as assignment: 3*10000000 equivalent to 3 seconds
1 1, MS (ms); 11, µs (subtlety); 11,the,the NS (nanosecond); 11, he,He,He, PS (leather seconds);
of this example
Code files
Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ty PE TForm1 = Class (Tform) Button1:tbutton; Procedure Button1Click (Sender:tobject); Procedure Formdestroy (Sender:tobject); End;var form1:tform1;implementation{$R *.dfm}var F:integer; Hwaitabletimer:thandle;function Mythreadfun (p:pointer): DWORD; Stdcall;var I, Y:integer; Y:= 20* F; If WaitForSingleObject (Hwaitabletimer, INFINITE) =wait_object_0 then begin for i:= 0 to $ do begin Form1.can Vas. Lock; Form1.Canvas.TextOut (x, Y, IntToStr (i)); Form1.Canvas.UnLock; Sleep (1); End End result:= 0;end;procedure Tform1.button1click (sender:tobject); var Threadid:dword; Duetime:int64;begin hwaitabletimer:= CreateWaitableTimer (nil, True, nil); Duetime:= -3*10000000; 3 seconds after execution of SetWaitableTimer (Hwaitabletimer, duetime, 0, Nil, nil, False); Repaint; f:= 0; CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); End;procedure Tform1.formdestroy (sender:tobject); begin CloseHandle (Hwaitabletimer); end;end.
Form files
Object Form1:tform1 Left = 0 Top = 0 Caption = ' Form1 ' clientheight = $ clientwidth = 179 Color = Clbtnface font.charset = default_charset font.color = clwindowtext font.height = -11 font.name = ' Tahoma ' font.style = [] oldcreateorder = False OnDestroy = Formdestroy pixelsperinch = TextHeight = Object Button1:tbutton left =-----------Width =---------- Caption = ' Button1 ' taborder = 0 OnClick = Button1Click endend
When we need an absolute time, such as 2009-2-18 13:10:5, the function needs the Int64 value should be the time of the tfiletime format
See the definition of three related time types (Tfiletime, Tsystemtime, Tdatetime) first
Tfiletime aka FILETIME or _filetime_filetime = record Dwlowdatetime:dword; Dwhighdatetime:dword;end;//tsystemtime also known as Sytemtime or _systemtime_systemtime=record Wyear:word; Wmonth:word; Wdayofweek:word; Wday:word; Whour:word; Wminute:word; Wsecond:word; Wmilliseconds:word;end; Tdatetime= type Double;
Tfiletime is equivalent to a Int64, which is usually assigned by tsystemtime or Tdatetime and then converted to the past
In the example, I converted the past through the following process:
Strtodatetime->datetimetosystemtime->systemtimetofiletime->localfiletimetofiletime
The following program executes three threads on February 18, 2009 1:10 P.M. 5 Seconds (Form IBID., I have found the right time to test successfully)
Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ty PE TForm1 = Class (Tform) Button1:tbutton; Procedure Button1Click (Sender:tobject); Procedure Formdestroy (Sender:tobject); End;var form1:tform1;implementation{$R *.dfm}var F:integer; Hwaitabletimer:thandle;function Mythreadfun (p:pointer): DWORD; Stdcall;var I,y:integer;begin Inc (f); Y: = * f; If WaitForSingleObject (Hwaitabletimer, INFINITE) = Wait_object_0 THEN begin for I: = 0 to + do begin FORM1. Canvas.lock; Form1.Canvas.TextOut (x, Y, IntToStr (i)); Form1.Canvas.Unlock; Sleep (1); End End Result: = 0;end;procedure Tform1.button1click (sender:tobject); Const Strtime = ' 2009-2-18 13:10:5 '; var Threadid:dword; Duetime:int64; St:tsystemtime; Ft,utc:tfiletime; Dt:tdatetime;begin Datetimetosystemtime (Strtodatetime (Strtime), ST); {from tdatetime to Tsystemtime} SystemTimeToFileTime (St, FT); {from tsystemtime to Tfiletime} LocalFileTimeToFileTime (ft, UTC); {from local time to international Standard Time UTC} Duetime: = Int64 (UTC); {The function requires Int64} hwaitabletimer: = CreateWaitableTimer (nil, True, nil); SetWaitableTimer (Hwaitabletimer, duetime, 0, Nil, nil, False); Repaint; F: = 0; CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); End;procedure Tform1.formdestroy (sender:tobject); begin CloseHandle (Hwaitabletimer); end;end.
The next step is the callback function for the Waitabletimer object.
Delphi multithreaded Programming (15)-Multi-threaded synchronization Waitabletimer (Waiting Timer object) [continued]