SetWaitableTimer定时器的使用

使用SetWaitableTimer定时器前先了解一下几个函数:

HANDLE CreateWaitableTimer(
  LPSECURITY_ATTRIBUTES lpTimerAttributes, //安全描述符,可以为NULL       
  BOOL bManualReset, //是否为手动定时器,如果是手动的,需要调用SetWaitableTimer才能将定
时器变成信号的,如果是自动的,则调用WaitForsingleObject即可实现定时器信号的重置
  LPCTSTR lpTimerName //定时器名称,这对于进程间的定时器来说是有用的。
);
BOOL bManualReset:用于指明人工重置的定时器或自动重置的定时器。当发出人工重置的定时器信号
通知时,等待该定时器的所有线程均变为可调度线程。当发出自动重置的定时器信号通知时,只有一
个等待的线程变为可调度线程。

BOOL SetWaitableTimer(
  HANDLE hTimer,                          // 定时器对象句柄
  const LARGE_INTEGER *pDueTime,          // 设定定时器从何时开始有信号
  LONG lPeriod,                           // 定时器周期
  PTIMERAPCROUTINE pfnCompletionRoutine,  // 回调函数
  LPVOID lpArgToCompletionRoutine,        // 传入回调函数参数
  BOOL fResume                         
);
HANDLE hTimer:定时器对象句柄
const LARGE_INTEGER *pDueTime:设定定时器从何时开始有信号,可

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用线程定时器实现的 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; } ``` 该代码使用了线程定时器来控制蛇的移动速度,通过 CreateWaitableTimer 和 SetWaitableTimer 函数来创建和设置定时器使用 CreateThread 函数来创建一个新线程并将 set_timer 函数作为入口点,通过 WaitForSingleObject 函数来等待线程执行完毕。 该代码实现了贪吃蛇的基本功能,包括蛇的移动、食物的随机生成、分数的计算、游戏结束等,可以在 Windows 环境下编译运行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值