[C++] 纯文本查看 复制代码
struct XSleep_Structure
{
int duration;
HANDLE evenHandle;
};
// 非阻塞延时
void XSleep(int nWaitInMsecs)
{
XSleep_Structure sleep;
sleep.duration = nWaitInMsecs;
sleep.evenHandle = CreateEvent(NULL,TRUE,FALSE,NULL);
DWORD dwThreadId;
CreateThread(NULL,0,&XSleepThread,&sleep,0,&dwThreadId);
MSG msg;
while(::WaitForSingleObject(sleep.evenHandle,0) == WAIT_TIMEOUT)
{
// get and dispatch message
if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
CloseHandle(sleep.evenHandle);
}
DWORD WINAPI XSleepThread(LPVOID pWaitTime)
{
XSleep_Structure *sleep = (XSleep_Structure*)pWaitTime;
Sleep(sleep->duration);
SetEvent(sleep->evenHandle);
return 0;
|