window c++ 定时器使用方法

定时器使用示例

使用定时器ID方式的使用方法

//定时器使用方式一
//每1秒,3秒开始执行
DWORD  WINAPI mytimernull(LPVOID args)
{
    BOOL bRet = FALSE;
    MSG msg = { 0 };
    
    //步骤一:创建定时器
    //如果hWnd为NULL,返回值为新建立的timer的ID
    UINT timerid1 = SetTimer(NULL, 0, 1000, NULL);
    UINT timerid2 = SetTimer(NULL, 0, 3000, NULL);

    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (bRet == -1)
        { 
            // handle the error and possibly exit 
        }
        else
        {
             //timer消息到达
             //步骤二:定时器处理程序
            if (msg.message == WM_TIMER)
            {
                //指定的timer 到达
                if (msg.wParam == timerid1)
                {
                    printf("timerid1 reaching ...\n");
                }

                //指定的timer 到达
                if (msg.wParam == timerid2)
                {
                    printf("timerid2 reaching ...\n\n\n");
                }
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }
    //步骤三:定时器销毁
    KillTimer(NULL, timerid1);
    KillTimer(NULL, timerid2);

    return 0;
}

使用回调函数方式的定时器使用方法

//定时器使用方式二
//步骤二:定时器处理程序
void CALLBACK Timerproc (
HWND hwnd,        // handle to window for timer messages 
UINT message,     // WM_TIMER message 
UINT idTimer,     // timer identifier 
DWORD dwTime     // current system time 
)
{
    printf("timerid3 reaching ...\n");
}

//每5秒开始执行
DWORD  WINAPI mytimerproc(LPVOID args)
{
     BOOL bRet = FALSE;
     MSG msg = { 0 };
     //步骤一:创建定时器
     //如果hWnd为NULL,返回值为新建立的timer的ID
     UINT timerid3 = SetTimer(NULL, 0, 5000, (TIMERPROC)Timerproc);

     while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
     {
         if (bRet == -1)
         {
             // handle the error and possibly exit 
         }
         else
         {
             //将定时器消息传递给so消息队列,这样才能触发Timerproc执行
             TranslateMessage(&msg);
             DispatchMessage(&msg);
         }
     }
     
     //步骤三:销毁定时器
     KillTimer(NULL, timerid3);
     return 0;
 }

//创建两个定时器线程

int main() 
{

    try
    {
        HANDLE handle1 = (HANDLE)::CreateThread(NULL, 0, mytimernull, NULL, 0, NULL);

        HANDLE handle2 = (HANDLE)::CreateThread(NULL, 0, mytimerproc, NULL, 0, NULL);

        HANDLE hArray[2] = { handle1 ,handle2 };
        WaitForMultipleObjects(2, hArray, TRUE, INFINITE);

        CloseHandle(handle1);
        CloseHandle(handle2);
    }
    catch (...)
    {
        printf("MonitorTeachingSoftPlayer error...\n");
    }

	return 0;
}

运行结果

timerid1 reaching …
timerid1 reaching …
timerid2 reaching …

timerid1 reaching …
timerid1 reaching …
timerid3 reaching …
timerid1 reaching …
timerid2 reaching …

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SetTimer 函数用于创建一个定时器,该定时器在指定的时间间隔后向消息队列发送一个定时器消息。其函数原型如下: ``` UINT_PTR SetTimer( HWND hWnd, UINT_PTR nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc ); ``` 其中,参数含义如下: - hWnd:指定定时器消息要发送到的窗口的句柄; - nIDEvent:指定定时器的 ID,可以通过此 ID 在以后的操作中引用该定时器; - uElapse:指定定时器的时间间隔,单位为毫秒; - lpTimerFunc:指向计时器过程的指针,当定时器消息发送到窗口时,系统会调用此过程。 下面是一个使用 SetTimer 函数的简单示例: ```c++ #include <windows.h> #define TIMER_ID 1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: SetTimer(hWnd, TIMER_ID, 1000, NULL); // 创建定时器,时间间隔为 1000ms break; case WM_TIMER: if (wParam == TIMER_ID) { MessageBox(NULL, TEXT("1 second has passed!"), TEXT("Timer"), MB_OK); } break; case WM_DESTROY: KillTimer(hWnd, TIMER_ID); // 销毁定时器 PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; HWND hWnd; MSG msg; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpszClassName = TEXT("TimerSample"); RegisterClassEx(&wcex); hWnd = CreateWindow(TEXT("TimerSample"), TEXT("Timer Sample"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } ``` 在这个示例中,我们创建一个定时器,在每隔 1 秒钟时弹出一个消息框。当窗口销毁时,我们通过 KillTimer 函数销毁定时器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值