请教标准c中如何实现定时器?windows下,不用vc,谢谢各位

mqm_2008 2006-02-20 10:10:58
最好有个例子,谢谢了
...全文
686 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
megatops 2006-02-21
  • 打赏
  • 举报
回复
实际上挂到系统的时钟中断上是个最直接通用的方法
cqpp 2006-02-21
  • 打赏
  • 举报
回复
先创建一个线程里面
while(1)
{
Sleep(x);啊,WaitForSingleObject啊,反正实现阻塞就行了。
然后 time_callback( void * param);
}
当然如果有多个定时器可以在线程里面自己改装一下。
可以吗?
mqm_2008 2006-02-21
  • 打赏
  • 举报
回复
这个,,不是标准c的把
cqpp 2006-02-21
  • 打赏
  • 举报
回复
timer.c

#include <windows.h>
#include "timer.h"

struct timer {
struct timer * next;
struct timer * prev;
LPCSTR timer_name;
LPVOID timer_param;
ULONG timer_id;
ULONG timer_counts;
ULONG timer_remain;
ULONG timer_status;
timer_callback tcb;
};

static timer timer_head = NULL;
static HANDLE hTimer = 0;
static DWORD dwTimer = 0;
static BOOL flag = TRUE;

#define INT_DISABLE
#define INT_ENABLE

static timer timer_find( timer tp )
{
timer p = timer_head;
timer rp = NULL;
while ( p ) {
if ( p->next == tp ) {
rp = p;
break;
}
p = p->next;
}

return rp;
}

static void timer_add( timer tp )
{
timer p = timer_find( NULL );
if ( p == NULL ){
timer_head = tp;
return;
}
else{
p->next = tp;
p->next->prev = p;
}
}

void timer_del( timer *tp )
{
timer p = *tp;

if ( ( tp == NULL ) || ( *tp == NULL ) ) {
return;
}
if ( p->prev ) {
p->prev->next = p->next;
}else {
timer_head = p->next;
}
if ( p->next ) {
p->next->prev = p->prev;
}

free( p );
*tp = NULL;
}

DWORD WINAPI timer_task( LPVOID param )
{
timer p = NULL;
while ( flag ) {
Sleep( 10 );
p = timer_head;
while ( p ) {
if ( ( p->timer_status & TIMER_ACTIVE ) && ( !--p->timer_remain ) ) {
p->timer_remain = p->timer_counts;
(p->tcb)(p->timer_param);
}
p = p->next;
}
}

return 0;
}

unsigned long timer_init( void )
{
hTimer = CreateThread( NULL, 0, timer_task, NULL, 0, &dwTimer );
if ( hTimer == NULL ) {
return 0;
}

return 1;
}

unsigned long timer_task_del( void )
{
timer p = NULL;
timer tp = NULL;
if ( hTimer != NULL ) {
flag = FALSE;
WaitForSingleObject( hTimer, INFINITE );
CloseHandle( hTimer );
hTimer = NULL;
dwTimer = 0;
}

p = timer_head;
while ( p ) {
tp = p->next;
free( p );
p = tp;
}
timer_head = NULL;
return 1;
}

timer timer_create( const char * name, timer_callback cb, void * param, unsigned long ticks, unsigned long remain, unsigned long status )
{
timer p = NULL;
p = malloc( sizeof( struct timer ) );
if ( p != NULL ) {
p->next = NULL;
p->prev = NULL;
p->tcb = cb;
p->timer_param = param;
p->timer_counts = ticks;
p->timer_remain = remain;
p->timer_status = status;
p->timer_name = name;
timer_add( p );
}
return p;
}

timer.h

#ifndef __TIMER_H__
#define __TIMER_H__

typedef void (*timer_callback)(void *);
typedef struct timer * timer;
#define TIMER_INACTIVE 0x00000000UL
#define TIMER_ACTIVE 0x00000001UL

unsigned long timer_init( void );
timer timer_create( const char * name, timer_callback cb, void * param, unsigned long ticks, unsigned long remain, unsigned long status );
void timer_del( timer *tp );
unsigned long timer_task_del( void );

#endif /*__TIMER_H__*/

自己可以再增加一些接口之类的。


test.c
#include <windows.h>
#include <stdio.h>
#include "timer.h"

void ctimer_expire( void * p )
{
printf( "ctimer_expire\n" );
}

void ptimer_expire( void * p )
{
printf( "ptimer_expire\n" );
}

void ttimer_expire( void * p )
{
printf( "ttimer_expire\n" );
}


void main()
{
timer ctimer = NULL;
timer ptimer = NULL;
timer ttimer = NULL;

timer_init();
ctimer = timer_create( "ctimer_expire", ctimer_expire, NULL, 100, 100, TIMER_ACTIVE );
ptimer = timer_create( "ptimer_expire", ptimer_expire, NULL, 30, 30, TIMER_ACTIVE );
ttimer = timer_create( "ttimer_expire", ttimer_expire, NULL, 20, 20, TIMER_ACTIVE );
while ( 1 )
{
Sleep(1000);
}
timer_task_del();
}
mqm_2008 2006-02-21
  • 打赏
  • 举报
回复
想找一个简单一些的方法,呵呵 复杂的我不太会 :(
mqm_2008 2006-02-20
  • 打赏
  • 举报
回复
#include "Windows.h"
#include "Mmsystem.h" //这一行可以不用
LPTIMECALLBACK on_timer();
main()
{
MMRESULT timer_id;
timer_id=timeSetEvent(100000,1,on_timer(), NULL,TIME_ONESHOT);

}
LPTIMECALLBACK on_timer(MMRESULT timer_id)
{
printf("Time Out event!");
timeKillEvent(timer_id);
return 0;
}
老大,这样写对吗?运行的时候好像根本没有计时,直接就打印了
cenlmmx 2006-02-20
  • 打赏
  • 举报
回复
#include "Windows.h"
#include "Mmsystem.h" //放后面
mqm_2008 2006-02-20
  • 打赏
  • 举报
回复
#include "Mmsystem.h"
#include "Windows.h"

LPTIMECALLBACK on_timer();
main()
{
MMRESULT timer_id;
timer_id=timeSetEvent(10000,1,on_timer(), NULL,TIME_ONESHOT);

}
LPTIMECALLBACK on_timer()
{
printf("Time Out event!");
}
错误多多啊,是不是可以这样写啊?都是这一类的错误,我用的是vc的编译环境
d:\program files\microsoft visual studio\vc98\include\mmsystem.h(113) : error C2061: syntax error : identifier 'MMVERSION'
d:\program files\microsoft visual studio\vc98\include\mmsystem.h(113) : error C2059: syntax error : ';'
d:\program files\microsoft visual studio\vc98\include\mmsystem.h(117) : error C2061: syntax error : identifier 'MMRESULT'
d:\program files\microsoft visual studio\vc98\include\mmsystem.h(117) : error C2059: syntax error : ';'
d:\program files\microsoft visual studio\vc98\include\mmsystem.h(121) : error C2061: syntax error : identifier 'FAR'
d:\program files\microsoft visual studio\vc98\include\mmsystem.h(121) : error C2059: syntax error : ';'
mqm_2008 2006-02-20
  • 打赏
  • 举报
回复
我正在试,谢谢
cenlmmx 2006-02-20
  • 打赏
  • 举报
回复
MSDN上:
Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Mmsystem.h; include Windows.h.
Library: Use Winmm.lib.

可以啊
qybao 2006-02-20
  • 打赏
  • 举报
回复
have a try

iTimerID = SetTimer (NULL, 0, wMsecInterval, TimerProc) ;

KillTimer (NULL, iTimerID) ;
mqm_2008 2006-02-20
  • 打赏
  • 举报
回复
我的硬件要求我用win2000 server
mqm_2008 2006-02-20
  • 打赏
  • 举报
回复
Requirements

Windows XP: Included in Windows XP only.
Header: Declared in Mmsystem.h; include Windows.h.
Library: Use Winmm.lib.

难道只有winxp支持?唉
mqm_2008 2006-02-20
  • 打赏
  • 举报
回复
谢谢,正在学习
cenlmmx 2006-02-20
  • 打赏
  • 举报
回复
同意使用timeSetEvent()函数,该函数原型如下:
MMRESULT timeSetEvent(UINT uDelay,UINT uResolution,LPTIMECALLBACK lpTimeProc,
DWORD dwUser,UINT fuEvent);
该函数的参数说明如下:
参数uDelay表示延迟时间;
参数uResolution表示时间精度,在Windows中缺省值为1ms;
lpTimeProc表示回调函数,为用户自定义函数,定时调用;
参数dwUser表示用户提供的回调数据;
参数fuEvent为定时器的事件类型,
zez 2006-02-20
  • 打赏
  • 举报
回复
定时器都是和系统相关的 ..

标准C 里没有定时器相关的东西,必须调用系统接口!!!

Windows下调用 timeSetEvent()
tuxw 2006-02-20
  • 打赏
  • 举报
回复
以前看别人写的一个例子

#include <windows.h>
#include <iostream>
using namespace std;

const UINT uiTimerID = 10;

VOID CALLBACK FooTimerFun( HWND, UINT, UINT, DWORD )
{
static int nCount = 0;
cout << "Timer Function , nCount = " << nCount ++ << endl;
if( nCount > 5 )
PostQuitMessage(0);
}

int main()
{
MSG msg;

SetTimer(NULL, uiTimerID, 1000, FooTimerFun);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

KillTimer(NULL, uiTimerID);

return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧