相当于零负担的延时程序(转)

{=============================================}
//过程 SuperWait()
//说明 超级延时()
//功能 相当于零负担的延时
//参数 nInterval:延时间隔
//     tUnit:时间单位 1:毫秒;2:秒
//
//例子 SuperWait(3,2); //延时3秒
//     SuperWait(5000); //延时5秒
//创建日期 2011-02-05
//修改日期 2011-02-05
{=============================================}


procedure SuperWait(nInterval: Cardinal; tUnit: Byte = 1); stdcall;
var
IdWait:
string;
hTimer, lBusy: Cardinal;
liDueTime: Int64;

begin
IdWait :
= FormatDateTime('HNSZ', Time());
hTimer :
= CreateWaitableTimer(nil, False, PAnsiChar(IdWait));
{
CreateWaitableTimerW(
__in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,
__in BOOL bManualReset,
__in_opt LPCWSTR lpTimerName
);
lpTimerAttributes 设置定时器的属性。
bManualReset 是否手动复位。
lpTimerName 定时器的名称。
}
if hTimer = 0 then Exit;
case tUnit of
1: //毫秒
liDueTime :
= nInterval * -10 * 1000;
2: //
liDueTime :
= nInterval * -10 * 1000 * 1000;
end;

try
SetWaitableTimer(hTimer, liDueTime,
0, nil, nil, False);
{
SetWaitableTimer(
__in HANDLE hTimer,
__in const LARGE_INTEGER *lpDueTime,
__in LONG lPeriod,
__in_opt PTIMERAPCROUTINE pfnCompletionRoutine,
__in_opt LPVOID lpArgToCompletionRoutine,
__in BOOL fResume
);
hTimer 定时器的句柄。
lpDueTime 设置定时器时间间隔,当设置为正值是绝对时间;
当设置为负数是相对时间。
lPeriod 间隔周期。
pfnCompletionRoutine 设置回调函数。
lpArgToCompletionRoutine 传送给回调函数的参数。
fResume 设置系统是否自动恢复。
}
repeat
lBusy :
= MsgWaitForMultipleObjects(1, hTimer, False, INFINITE, QS_ALLINPUT);
{
DWORD WINAPI MsgWaitForMultipleObjects(
__in DWORD nCount,
__in const HANDLE* pHandles,
__in BOOL bWaitAll,
__in DWORD dwMilliseconds,
__in DWORD dwWakeMask

nCount 指定列表中的句柄数量。
pHandles 指定对象句柄组合中的第一个元素。
fWaitAll 如果为TRUE,表示除非对象同时发出信号,否则就等待下去。
如果为FALSE,表示任何对象发出信号即可。
dwMilliseconds 指定要等待的毫秒数。
dwWakeMask 带有QS_??前缀的一个或多个常数,用于标识特定的消息
类型。
}
Application.ProcessMessages;
until lBusy = WAIT_OBJECT_0;
CloseHandle(hTimer);
except
CloseHandle(hTimer);
end;

end;

posted @ 2011-02-18 11:56  dwbboy  阅读(196)  评论(0编辑  收藏  举报