当于零负担的延时程序
时间:2011-02-05 来源:Moodsky

1 procedure SuperWait(nInterval: Cardinal; tUnit: Byte = 1); stdcall;
2 var
3 IdWait: string;
4 hTimer, lBusy: Cardinal;
5 liDueTime: Int64;
6
7 begin
8 IdWait := FormatDateTime('HNSZ', Time());
9 hTimer := CreateWaitableTimer(nil, False, PAnsiChar(IdWait));
10 {
11 CreateWaitableTimerW(
12 __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,
13 __in BOOL bManualReset,
14 __in_opt LPCWSTR lpTimerName
15 );
16 lpTimerAttributes 设置定时器的属性。
17 bManualReset 是否手动复位。
18 lpTimerName 定时器的名称。
19 }
20 if hTimer = 0 then Exit;
21 case tUnit of
22 1: //毫秒
23 liDueTime := nInterval * -10 * 1000;
24 2: //秒
25 liDueTime := nInterval * -10 * 1000 * 1000;
26 end;
27
28 try
29 SetWaitableTimer(hTimer, liDueTime, 0, nil, nil, False);
30 {
31 SetWaitableTimer(
32 __in HANDLE hTimer,
33 __in const LARGE_INTEGER *lpDueTime,
34 __in LONG lPeriod,
35 __in_opt PTIMERAPCROUTINE pfnCompletionRoutine,
36 __in_opt LPVOID lpArgToCompletionRoutine,
37 __in BOOL fResume
38 );
39 hTimer 定时器的句柄。
40 lpDueTime 设置定时器时间间隔,当设置为正值是绝对时间;
41 当设置为负数是相对时间。
42 lPeriod 间隔周期。
43 pfnCompletionRoutine 设置回调函数。
44 lpArgToCompletionRoutine 传送给回调函数的参数。
45 fResume 设置系统是否自动恢复。
46 }
47 repeat
48 lBusy := MsgWaitForMultipleObjects(1, hTimer, False, INFINITE, QS_ALLINPUT);
49 {
50 DWORD WINAPI MsgWaitForMultipleObjects(
51 __in DWORD nCount,
52 __in const HANDLE* pHandles,
53 __in BOOL bWaitAll,
54 __in DWORD dwMilliseconds,
55 __in DWORD dwWakeMask
56
57 nCount 指定列表中的句柄数量。
58 pHandles 指定对象句柄组合中的第一个元素。
59 fWaitAll 如果为TRUE,表示除非对象同时发出信号,否则就等待下去。
60 如果为FALSE,表示任何对象发出信号即可。
61 dwMilliseconds 指定要等待的毫秒数。
62 dwWakeMask 带有QS_??前缀的一个或多个常数,用于标识特定的消息
63 类型。
64 }
65 Application.ProcessMessages;
66 until lBusy = WAIT_OBJECT_0;
67 CloseHandle(hTimer);
68 except
69 CloseHandle(hTimer);
70 end;
71
72 end;
相关阅读 更多 +
排行榜 更多 +