线程池计时器

来源:互联网 发布:童瑶事件 知乎 编辑:程序博客网 时间:2024/05/21 19:24
#include "stdafx.h"#include <StrSafe.h>#ifdef _DEBUG#define new DEBUG_NEW#endif#define ID_MSGBOX_STATIC_TEXT 0x0000ffffTCHAR g_szCaption[100];int g_nSecLeft = 0;void CALLBACK MsgBoxTimeoutCallback(PTP_CALLBACK_INSTANCE pInstance,PVOID pvContext,PTP_TIMER pTimer){HWND hwnd = FindWindow(NULL, g_szCaption);if (hwnd != NULL){if (g_nSecLeft == 1){EndDialog(hwnd, IDOK);return;}TCHAR szMsg[100];StringCchPrintf(szMsg, _countof(szMsg), TEXT("You have %d seconds to respond"),--g_nSecLeft);SetDlgItemText(hwnd, ID_MSGBOX_STATIC_TEXT, szMsg);}}// 唯一的应用程序对象CWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){int nRetCode = 0;// 初始化 MFC 并在失败时显示错误if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){// TODO: 更改错误代码以符合您的需要_tprintf(_T("错误: MFC 初始化失败\n"));nRetCode = 1;}else{// TODO: 在此处为应用程序的行为编写代码。_tcscpy_s(g_szCaption, 100, TEXT("Timed Message Box"));g_nSecLeft = 10;PTP_TIMER lpTimer = CreateThreadpoolTimer(MsgBoxTimeoutCallback, NULL, NULL);if (lpTimer == NULL){TCHAR szMsg[MAX_PATH];StringCchPrintf(szMsg, _countof(szMsg),TEXT("Impossible to create the timer: %u"), GetLastError());MessageBox(NULL, szMsg, TEXT("Error"), MB_OK|MB_ICONERROR);return -1;}ULARGE_INTEGER ulRelativeStartTime;ulRelativeStartTime.QuadPart = (LONGLONG) - (10000000);FILETIME ftRelativeStartTime;ftRelativeStartTime.dwHighDateTime = ulRelativeStartTime.HighPart;ftRelativeStartTime.dwLowDateTime  = ulRelativeStartTime.LowPart;SetThreadpoolTimer(lpTimer,&ftRelativeStartTime,1000,0);MessageBox(NULL, TEXT("You have 10 seconds to respond"), g_szCaption, MB_OK);CloseThreadpoolTimer(lpTimer);MessageBox(NULL, (g_nSecLeft == 1) ? TEXT("TimeOut") : TEXT("User Responded"),TEXT("Result"), MB_OK);}return nRetCode;}

原创粉丝点击