zoukankan      html  css  js  c++  java
  • 计时器

     1 // 计时器.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include <windows.h>
     6 #include <iostream>
     7 #include <time.h>
     8 using namespace std;
     9 
    10 BOOL __IsLoop = TRUE;
    11 
    12 
    13 void GetSystemTime();
    14 DWORD WINAPI ThreadProc(LPVOID parameterData);
    15 
    16 
    17 int main()
    18 {
    19     HANDLE ThreadHandle = CreateThread(
    20         NULL,
    21         0,
    22         (LPTHREAD_START_ROUTINE)ThreadProc,
    23         NULL,
    24         0,
    25         NULL
    26         );
    27 
    28     printf("input any key to exit
    ");
    29     getchar();
    30     __IsLoop = FALSE;
    31     WaitForSingleObject(ThreadHandle, INFINITE);
    32 
    33     if (ThreadHandle != NULL)
    34     {
    35         CloseHandle(ThreadHandle);
    36         ThreadHandle = NULL;
    37     }
    38 
    39     
    40     return 0;
    41 }
    42 
    43 DWORD WINAPI ThreadProc(LPVOID parameterData)
    44 {
    45     LARGE_INTEGER DueTime;
    46     DueTime.QuadPart = -10'000'000LL;//倒计时1秒
    47 
    48     HANDLE TimeHandle = NULL;
    49     TimeHandle =
    50         CreateWaitableTimer(
    51             NULL,
    52             //TRUE, //BOOL  bManualReset
    53             FALSE,
    54             NULL
    55         );
    56 
    57 
    58     //同步事件(synchronizationEvent)
    59     //当事件对象为激发时,如遇到KeWaitForXX等内核函数,事件对象则自动变回未激发态
    60     //通知事件(NotificationEvent)
    61     //当事件对象为激发时,如遇到KeWaitForXX等内核函数,事件对象则不会自动变回未激发态
    62     
    63     while (__IsLoop)
    64     {
    65         if (!SetWaitableTimer(TimeHandle, &DueTime, 0, NULL, NULL, 0))
    66         {
    67             printf("SetWaitableTimer FAIL: %d  
    ", GetLastError());
    68             return 0;
    69         }
    70 
    71         BOOL IsOk = WaitForSingleObject(TimeHandle, INFINITE);
    72         IsOk -= WAIT_OBJECT_0;
    73 
    74         if (IsOk==0)
    75         {
    76             system("cls");
    77             GetSystemTime();
    78             printf("Timer was signaled
    ");
    79         }
    80         else
    81         {
    82             break;
    83         }
    84     }
    85     CancelWaitableTimer(TimeHandle);
    86     CloseHandle(TimeHandle);
    87     TimeHandle = NULL;
    88     printf("ThreadProc Exit
    ");
    89 
    90     return 0;
    91 }
    92 
    93 void GetSystemTime()
    94 {
    95     time_t t = time(0);
    96     char tmp[64];
    97     strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z", localtime(&t));
    98     puts(tmp);
    99 }
  • 相关阅读:
    mysql查看所有的数据库
    查看/杀死mysql进程
    PL/SQL 保存页面布局
    Linux 隐藏文件和目录,显示隐藏的文件和目录
    win10插入U盘多显示了一个盘符
    Linux安装rz和sz命令
    PL/SQL链接Oracle出现乱码
    解决Tomcat: Can't load IA 32-bit .dll on a AMD 64-bit platform问题
    Eclipse集成Tomcat的配置步骤实例
    搭建Eclipse+MyEclipse开发环境
  • 原文地址:https://www.cnblogs.com/1228073191Blog/p/7496440.html
Copyright © 2011-2022 走看看