zoukankan      html  css  js  c++  java
  • CreateWaitableTimer和SetWaitableTimer

    负值表示相对时间,正值表示绝对时间,定时器精度为100ns (1ns=1/10亿 s),所以 -50000000 代表5秒,详见MSDN。

    程序一为自动重置(先等待5秒,然后每1秒输出一次):

    #include "stdafx.h"
    #include<Windows.h>
    #include<iostream>
    #include<time.h>
    
    using namespace std;
    int main(){
        LARGE_INTEGER li;   
        li.QuadPart = -50000000;  
    
         HANDLE hTimer = CreateWaitableTimer( NULL,FALSE,NULL ); 
        if( !SetWaitableTimer( hTimer,&li,1000,NULL,NULL,0 )) {  
            cout<<"error"<<endl;
            CloseHandle( hTimer );   
            return 0;   
        }  
        while ( 1 ){  
            clock_t c_beg = clock();  
            WaitForSingleObject(hTimer,INFINITE);
            clock_t end = clock() - c_beg;  
            cout<<"time:"<<end<<endl;   
        }  
        CloseHandle(hTimer);  
        system("pause");
        return 0;
    }

    程序二为手动重置(每秒输出),其实当CreateWaitableTimer第二个参数为TRUE时(即手动重置),SetWaitableTimer的第三个参数是不起作用的

    #include "stdafx.h"
    #include<Windows.h>
    #include<iostream>
    #include<time.h>
    
    using namespace std;
    int main(){
        LARGE_INTEGER li;   
        li.QuadPart = -10000000;  
    
         HANDLE hTimer = CreateWaitableTimer( NULL,TRUE,NULL ); 
        if( !SetWaitableTimer( hTimer,&li,1000,NULL,NULL,0 )) {  
            cout<<"error"<<endl;
            CloseHandle( hTimer );   
            return 0;   
        }  
        while ( 1 ){  
            clock_t c_beg = clock();  
            WaitForSingleObject(hTimer,INFINITE);
            SetWaitableTimer( hTimer,&li,1000,NULL,NULL,0 );
            clock_t end = clock() - c_beg;  
            cout<<"time:"<<end<<endl;   
        }  
        CloseHandle(hTimer);  
        system("pause");
        return 0;
    }

     程序三:APC(异步调用过程)加入定时器

    见MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/ms686898%28v=vs.85%29.aspx

  • 相关阅读:
    python学习-dict
    python学习
    pycharm 2017版Mac激活码
    Day6_python基础知识<模块学习>
    having 子句
    数据库实例指定
    EXCEL里面单元格内容太多显示不全应该怎么弄。
    你没有权限在此位置保存文件_请与管理员联系的问题解决
    FQ软件
    C#高级编程(中文第七版)
  • 原文地址:https://www.cnblogs.com/duyy/p/3755308.html
Copyright © 2011-2022 走看看