The second parameter (starting time) for the next setwaitabletimer is specifically studied.
It has positive, negative, 0-valued three cases, preceded by a value of 0.
Learn about negative values (relative time), that is, how long it will take to start execution from the current count.
This relative time is 1/100 nanoseconds, for example, the assignment 3*10000000 is equivalent to 3 seconds.
1 s(秒) = 1,000 ms(毫秒);
1 s(秒) = 1,000,000 µs(微妙);
1 s(秒) = 1,000,000,000 ns(纳秒);
1 s(秒) = 1,000,000,000,000 ps(皮秒);
This example effect chart:
Code files:
Unit Unit1
Interface
uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms ,
Dialogs, Stdctrls;
Type
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 1000 do
B Egin
Form1.Canvas.Lock;
Form1.Canvas.TextOut (M, 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 to execute}
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 file:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 116
ClientWidth = 179
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 96
Top = 83
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end