栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

vc6.0中c语言控制台程序中的定时技术(定时器)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vc6.0中c语言控制台程序中的定时技术(定时器)

打开main.c编译运行,注意,打开main.c之后一定要将win32timer.c也加进工程中一起编译,下面有图。
在开发单片机、ARM以及Linux系统的程序时,因为硬件定时中断的存在我们很方便构造出定时ISR,然而在VC6.0中,我们如何写一个定时程序呢?
其实,就是timeSetEvent()这个函数的调用。这个函数的解释见MSDN。详细原理,请看我代码中的注释,我写得很详细了。

main.c
复制代码 代码如下:
//======================
// main.c
//======================
#include
#include "win32timer.h"  // UserTimerSet(uDelay,UserFun)

int cnt = 0;

void myISR_Called_Per_1000ms(void);

int main(void)
{
 
 UserTimerSet ( 1000, myISR_Called_Per_1000ms ) ;

 while (cnt<10);

 return 0; 
}

void myISR_Called_Per_1000ms(void)

 printf("The Program has run %dsn",cnt++);
}

win32timer.h
复制代码 代码如下:


//=======================
// win32timer.h
//=======================
#ifndef __WIN32TIMER_H__
#define __WIN32TIMER_H__

void UserTimerSet ( unsigned int uDelay, void (*UserFun)(void) ) ;

#endif  // @ #ifndef __WIN32TIMER_H__

win32timer.c
复制代码 代码如下:
//=======================
// win32timer.c
//=======================

#include 
#include "win32timer.h"

#pragma comment(lib,"winmm.lib") //导入winmm.lib多媒体库


HANDLE mainhandle;     //主线程句柄
ConTEXT Context;     //主线程切换上下文
static void (*TimerCallFun)(void);  //声明用户调用函数指针


static void __stdcall TimerISR(unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2);

//======================================================================================
// 函数功能:用户需要调用的定时器设置(初始化)函数
// 入口参数:uDelay:定时器定时时长,单位为ms
//     void (*UserFun)(void):指向用户函数 void fun (void) 的函数指针
// 返 回 值:无
//======================================================================================
void UserTimerSet ( unsigned int uDelay, void (*UserFun)(void) )
{
 HANDLE cp,ct;

 TimerCallFun = UserFun;     //得到用户被定时调用的函数的函数指针
 Context.ContextFlags = CONTEXT_CONTROL;
 cp = GetCurrentProcess(); //得到当前进程句柄
 ct = GetCurrentThread(); //得到当前线程伪句柄
 DuplicateHandle( cp, ct, cp, &mainhandle, 0, TRUE, 2 ); //伪句柄转换,得到线程真句柄

 
 timeSetEvent( uDelay, 0, TimerISR, 0, TIME_PERIODIC );
 
}

//======================================================================================
// 函数功能:timeSetEvent需要定时调用的函数
// 入口参数:unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2,详见MSDN
// 返 回 值:无
//======================================================================================
static void __stdcall TimerISR(unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2)
{
 SuspendThread(mainhandle); //中止主线程的运行,模拟中断产生.但没有保存寄存器
 GetThreadContext(mainhandle, &Context); //得到主线程上下文,为切换任务做准备
 //===========================================================================================
 (*TimerCallFun)();    //或者TimerCallFun(); ———— 用户自定义实现的中断调用
 //===========================================================================================
 ResumeThread(mainhandle); //模拟中断返回,主线程得以继续执行
}


工程图

运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号