zoukankan      html  css  js  c++  java
  • 2016-07-15: Window定时器使用

    windows下定时器使用实例

    #include <iostream>
    #include <Windows.h>
    
    using namespace std;
    
    void TimerDemo()
    {
        int count = 0;    // Iteration counter
    
        HANDLE hTimer = NULL;    // WaitableTimer handle
        hTimer = CreateWaitableTimer(    // Create waitable timer
            NULL,
            FALSE,    // Autoreset --> timer restarts counting after event fired
            NULL);
    
        SYSTEMTIME time;        // System time structure
        GetSystemTime(&time);    // Curren time
        time.wSecond += 3;        // Wait 3 sec
    
        FILETIME ftime;    // File time (expected by SetWaitableTimer)
        SystemTimeToFileTime(&time, &ftime);    // Convert system to file time
    
    
        if(!SetWaitableTimer(    // Set waitable timer
            hTimer,    // Timer handle
            reinterpret_cast<LARGE_INTEGER*>(&ftime),    // Convert file time to large integer
            200,    // Period time, fire again after 200ms
            NULL, 
            NULL,
            0))
        {
            cout << "SetWaitableTimer failed" <<  GetLastError() << endl;
        };
    
        cout << "Start Time " << GetTickCount() << endl;
    
        while(WaitForSingleObject(hTimer, 5000) == WAIT_OBJECT_0){    // Wait for timer event
            cout << "CALLED " << ++count << endl;
            if(count+1 > 50){    // Exit after 5 events
                break;
            }
        }
    
        cout << "End Time " << GetTickCount() << endl;
    
        CancelWaitableTimer(hTimer);    // Stop timer
        CloseHandle(hTimer);            // Delete handle
    
    }
  • 相关阅读:
    HDU1548:A strange lift
    hdu1978_How many ways dfs+记忆化搜索
    HDU1518:Square(DFS)
    HDU ACM 1495 非常可乐(广搜BFS)
    HDU1372:Knight Moves(BFS)
    662_二叉树最大宽度
    590_N叉树的后序遍历
    一直在坚持
    动态链接库DLL的操作顺序
    最近木马的查杀方法/各类杀毒软件的使用
  • 原文地址:https://www.cnblogs.com/zhouLee/p/5674136.html
Copyright © 2011-2022 走看看