tttime.h
1 2 3 4 5 6 7 8 9 |
```C++ __pragma("once") #include <Windows.h> #ifndef _XSLEEP_H_ #define _XSLEEP_H_ void XSleep(int nWaitInMSecs); #endif // _XSLEEP_H_ //非阻塞版本的延时实现(区别于Sleep()) ``` |
tttime.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include "tttime.h" struct XSleep_Structure { //int duration; //HANDLE eventHandle; int duration {0}; HANDLE eventHandle { nullptr }; }; DWORD WINAPI XSleepThread(LPVOID pWaitTime) { //XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime; XSleep_Structure *sleep = static_cast<XSleep_Structure *>(pWaitTime); Sleep(sleep->duration); SetEvent(sleep->eventHandle); return 0; } void XSleep(int nWaitInMSecs) { XSleep_Structure sleep; sleep.duration = nWaitInMSecs; sleep.eventHandle = CreateEvent(NULL, TRUE, FALSE, NULL); DWORD threadId; CreateThread(NULL, 0, &XSleepThread, &sleep, 0, &threadId); MSG msg; while (::WaitForSingleObject(sleep.eventHandle, 0) == WAIT_TIMEOUT) { if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } CloseHandle(sleep.eventHandle); } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ MFC 自定义消息04/29
- ♥ 实用操作一10/02
- ♥ 使用Cmake构建工程:一05/17
- ♥ C++_函数模板、类模板、特化、模板元编程、SFINAE、概念06/22
- ♥ Gitlab CI 相关记述06/20
- ♥ Windows 核心编程 _ 用户模式:线程同步二07/16