CreateWaitableTimer和SetWaitableTimer函数

  用户感觉到软件的好用,就是可以定时地做一些 工作 ,而不需要人参与进去。比如每天定时地升级病毒库,定时地下载电影,定时地更新游戏里的人物。要想实现这些功能,就可以使用定时器的 API 函数 CreateWaitableTimer SetWaitableTimer 来实现了,这对 API 函数创建的时钟是比较精确的,可以达到 100 倍的 10 亿分之一秒。
 
函数 CreateWaitableTimer SetWaitableTimer 声明如下:
 
WINBASEAPI
__out
HANDLE
WINAPI
CreateWaitableTimerA(
    __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,
    __in     BOOL bManualReset,
    __in_opt LPCSTR lpTimerName
    );
WINBASEAPI
__out
HANDLE
WINAPI
CreateWaitableTimerW(
    __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,
    __in     BOOL bManualReset,
    __in_opt LPCWSTR lpTimerName
    );
#ifdef UNICODE
#define CreateWaitableTimer CreateWaitableTimerW
#else
#define CreateWaitableTimer CreateWaitableTimerA
#endif // !UNICODE
 
 
WINBASEAPI
BOOL
WINAPI
SetWaitableTimer(
    __in     HANDLE hTimer,
    __in     const LARGE_INTEGER *lpDueTime,
    __in     LONG lPeriod,
    __in_opt PTIMERAPCROUTINE pfnCompletionRoutine,
    __in_opt LPVOID lpArgToCompletionRoutine,
    __in     BOOL fResume
    );
 
lpTimerAttributes 是设置定时器的属性。
bManualReset 是是否手动复位。
lpTimerName 是定时器的名称。
hTimer 是定时器的句柄。
lpDueTime 是设置定时器时间间隔,当设置为正值是绝对时间;当设置为负数是相对时间。
lPeriod 是周期。
pfnCompletionRoutine 是设置回调函数。
lpArgToCompletionRoutine 是传送给回调函数的参数。
fResume 是设置系统是否自动恢复。
 
调用函数的例子如下:
#001 // 创建定时器
#002  // 蔡军生  2007/11/06 QQ:9073204 深圳
#003  int CreateTestTimer(void)
#004  {
#005         HANDLE hTimer = NULL;
#006         LARGE_INTEGER liDueTime;
#007 
#008         // 设置相对时间为 10 秒。
#009         liDueTime.QuadPart = -100000000;
#010 
#011      ;   //创建定时器。
#012        hTimer = CreateWaitableTimer(NULL, TRUE, _T("TestWaitableTimer"));
#013         if (!hTimer)
#014         {              
#015               return 1;
#016         }
#017 
#018         OutputDebugString(_T("10秒定时器/r/n"));
#019 
#020         // 设置10秒钟。
#021        if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
#022         {        
#023               //
#024               CloseHandle(hTimer);
#025               return 2;
#026         }
#027 
#028         //等定时器有信号。
#029         if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
#030         {
#031               OutputDebugString(_T("10秒定时器出错了/r/n"));   
#032               //
#033               CloseHandle(hTimer);
#034               return 3;
#035         }
#036         else
#037         {
#038               //10秒钟到达。
#039               OutputDebugString(_T("10秒定时器到了/r/n"));            
#040         }
#041 
#042         //
#043         CloseHandle(hTimer);
#044         return 0;
#045  } 
以下是使用线程定时器实现的 C语言贪吃蛇源代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 50 #define HEIGHT 20 #define SNAKE_LEN 5 #define FOOD_SCORE 10 #define SPEED 100 int score = 0; int rx = 0; int ry = 0; int over = 0; int map[HEIGHT][WIDTH] = {0}; int snake[SNAKE_LEN][2] = {0}; int dir = 1; // 0:up 1:right 2:down 3:left void init_map(); void init_snake(); void init_food(); void display(); void move(); void update_snake(int x, int y); void update_food(); void game_over(); void set_timer(); void init_map() { int i, j; for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) { map[i][j] = -1; } else { map[i][j] = 0; } } } } void init_snake() { int i; for (i = 0; i < SNAKE_LEN; i++) { snake[i][0] = 5; snake[i][1] = 5 + i; map[snake[i][0]][snake[i][1]] = i == SNAKE_LEN - 1 ? 1 : 2; } } void init_food() { srand((unsigned)time(NULL)); rx = rand() % (HEIGHT - 2) + 1; ry = rand() % (WIDTH - 2) + 1; if (map[rx][ry] != 0) { init_food(); } else { map[rx][ry] = -2; } } void display() { int i, j; system("cls"); printf("Score: %d\n", score); for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { switch (map[i][j]) { case 0: printf(" "); break; case -1: printf("#"); break; case -2: printf("*"); break; default: printf("o"); break; } } printf("\n"); } } void move() { int i, j; int x = snake[SNAKE_LEN - 1][0]; int y = snake[SNAKE_LEN - 1][1]; switch (dir) { case 0: x--; break; case 1: y++; break; case 2: x++; break; case 3: y--; break; } if (map[x][y] == -1 || map[x][y] >= 1) { game_over(); return; } update_snake(x, y); if (map[x][y] == -2) { score += FOOD_SCORE; update_food(); } else { map[snake[0][0]][snake[0][1]] = 0; for (i = 0; i < SNAKE_LEN - 1; i++) { snake[i][0] = snake[i + 1][0]; snake[i][1] = snake[i + 1][1]; } snake[SNAKE_LEN - 1][0] = x; snake[SNAKE_LEN - 1][1] = y; map[x][y] = SNAKE_LEN; } } void update_snake(int x, int y) { int i; map[snake[0][0]][snake[0][1]] = 0; for (i = 0; i < SNAKE_LEN - 1; i++) { snake[i][0] = snake[i + 1][0]; snake[i][1] = snake[i + 1][1]; } snake[SNAKE_LEN - 1][0] = x; snake[SNAKE_LEN - 1][1] = y; map[x][y] = SNAKE_LEN; } void update_food() { int i, j; rx = rand() % (HEIGHT - 2) + 1; ry = rand() % (WIDTH - 2) + 1; if (map[rx][ry] != 0) { update_food(); } else { map[rx][ry] = -2; } } void game_over() { over = 1; printf("Game Over!\n"); } void set_timer() { time_t t; t = time(NULL); while (1) { if (over == 1) { break; } if (time(NULL) - t >= SPEED / 1000) { move(); display(); t = time(NULL); } } } int main() { init_map(); init_snake(); init_food(); display(); HANDLE hTimer = NULL; LARGE_INTEGER liDueTime; liDueTime.QuadPart = 0; hTimer = CreateWaitableTimer(NULL, TRUE, NULL); SetWaitableTimer(hTimer, &liDueTime, SPEED, NULL, NULL, 0); HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)set_timer, NULL, 0, NULL); WaitForSingleObject(hThread, INFINITE); return 0; } ``` 该代码使用了线程定时器来控制蛇的移动速度,通过 CreateWaitableTimerSetWaitableTimer 函数来创建和设置定时器,使用 CreateThread 函数来创建一个新线程并将 set_timer 函数作为入口点,通过 WaitForSingleObject 函数来等待线程执行完毕。 该代码实现了贪吃蛇的基本功能,包括蛇的移动、食物的随机生成、分数的计算、游戏结束等,可以在 Windows 环境下编译运行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值