最近在学Ardence RTX实时系统软件,有很多不懂的地方,下面是一个定时器程序,不太明白?请路过大虾们帮忙看看。
下面是源代码,包括两个文件,一个头文件,一个源文件。如下所示:
//////////////////////////////////////////////////////////////////
//
// RTX_TIME_SLEEP.h - Header file
//
// This file was generated using the RTX 6.5.1 Application Wizard.
//
//////////////////////////////////////////////////////////////////#include <windows.h>
#include <wchar.h>
#include <rtapi.h>// function prototype for periodic timer function
void
RTFCNDCL
TimerHandler(
void * nContext
);
void print();.c文件
//////////////////////////////////////////////////////////////////
//
// RTX_TIME_SLEEP.c - C file
//
// This file was generated using the RTX 6.5.1 Application Wizard.
//
//////////////////////////////////////////////////////////////////
#include "rtx_time_sleep.h"
#include "windows.h"
#include "stdio.h"
#include "rtapi.h"LARGE_INTEGER Period; // Timer period
LARGE_INTEGER StartTime; // Start time of sampling run
ULONG TimerCounter = 0; // Counts entries to timer handler
void
_cdecl
wmain(
int argc,
wchar_t **argv,
wchar_t **envp
){ // for periodic timer code
LARGE_INTEGER x;
HANDLE hTimer; // timer handle // RTX periodic timer code:
// TO DO: Set default timer period to your desired time.
// The period needs to be an even multiple of the HAL
// period found in the control panel.
// This example uses a period of 500 micro seconds. Period.QuadPart = 500; if (!RtSetThreadPriority( GetCurrentThread(), RT_PRIORITY_MAX-1))
printf("WARNING: Can't set to highest RTAPI priority.\n");
// Create a periodic timer
if (! (hTimer = RtCreateTimer(
NULL, // security
0, // stack size - 0 uses default
TimerHandler, // timer handler
NULL, // NULL context (argument to handler)
RT_PRIORITY_MAX, // priority
CLOCK_2) )) // RTX HAL timer
{
//
// TO DO: exception code here
// RtWprintf(L"RtCreateTimer error = %d\n",GetLastError());
ExitProcess(2);
} if (! RtSetTimerRelative( hTimer,
&Period,
&Period))
{
ExitProcess(2);
} Sleep(50);
//
// Stop and delete the timer.
//
RtCancelTimer( hTimer, &x);
RtDeleteTimer( hTimer);
ExitProcess(0);
}
void
RTFCNDCL
TimerHandler(
PVOID unused
)
{
// TO DO: your timer handler code here
TimerCounter++;
print();
}void print()
{
printf("%d\n",TimerCounter);
}我的问题是,为什么必须加入Sleep()函数(即上面红色标记代码处),才能让上面创建的定时器函数调用程序调用TimerHandler()函数,不加的话,它就不执行。不像是windows 中的自己的定时器,它会自动每隔设定的时间去执行定时器指定的要调用的函数。大虾们请帮我看看,怎么改,才能让定时器自动调用TimerHandler()函数,就像windows中的一样,还有那个蓝色处的那两个参数是什么意思?理解的不是很清楚。谢谢,不知哪位大虾有没有这方面的资料,例程是最好了,发给我看看,我的邮箱是[email protected] 希望路过的大虾们多多指教
下面是源代码,包括两个文件,一个头文件,一个源文件。如下所示:
//////////////////////////////////////////////////////////////////
//
// RTX_TIME_SLEEP.h - Header file
//
// This file was generated using the RTX 6.5.1 Application Wizard.
//
//////////////////////////////////////////////////////////////////#include <windows.h>
#include <wchar.h>
#include <rtapi.h>// function prototype for periodic timer function
void
RTFCNDCL
TimerHandler(
void * nContext
);
void print();.c文件
//////////////////////////////////////////////////////////////////
//
// RTX_TIME_SLEEP.c - C file
//
// This file was generated using the RTX 6.5.1 Application Wizard.
//
//////////////////////////////////////////////////////////////////
#include "rtx_time_sleep.h"
#include "windows.h"
#include "stdio.h"
#include "rtapi.h"LARGE_INTEGER Period; // Timer period
LARGE_INTEGER StartTime; // Start time of sampling run
ULONG TimerCounter = 0; // Counts entries to timer handler
void
_cdecl
wmain(
int argc,
wchar_t **argv,
wchar_t **envp
){ // for periodic timer code
LARGE_INTEGER x;
HANDLE hTimer; // timer handle // RTX periodic timer code:
// TO DO: Set default timer period to your desired time.
// The period needs to be an even multiple of the HAL
// period found in the control panel.
// This example uses a period of 500 micro seconds. Period.QuadPart = 500; if (!RtSetThreadPriority( GetCurrentThread(), RT_PRIORITY_MAX-1))
printf("WARNING: Can't set to highest RTAPI priority.\n");
// Create a periodic timer
if (! (hTimer = RtCreateTimer(
NULL, // security
0, // stack size - 0 uses default
TimerHandler, // timer handler
NULL, // NULL context (argument to handler)
RT_PRIORITY_MAX, // priority
CLOCK_2) )) // RTX HAL timer
{
//
// TO DO: exception code here
// RtWprintf(L"RtCreateTimer error = %d\n",GetLastError());
ExitProcess(2);
} if (! RtSetTimerRelative( hTimer,
&Period,
&Period))
{
ExitProcess(2);
} Sleep(50);
//
// Stop and delete the timer.
//
RtCancelTimer( hTimer, &x);
RtDeleteTimer( hTimer);
ExitProcess(0);
}
void
RTFCNDCL
TimerHandler(
PVOID unused
)
{
// TO DO: your timer handler code here
TimerCounter++;
print();
}void print()
{
printf("%d\n",TimerCounter);
}我的问题是,为什么必须加入Sleep()函数(即上面红色标记代码处),才能让上面创建的定时器函数调用程序调用TimerHandler()函数,不加的话,它就不执行。不像是windows 中的自己的定时器,它会自动每隔设定的时间去执行定时器指定的要调用的函数。大虾们请帮我看看,怎么改,才能让定时器自动调用TimerHandler()函数,就像windows中的一样,还有那个蓝色处的那两个参数是什么意思?理解的不是很清楚。谢谢,不知哪位大虾有没有这方面的资料,例程是最好了,发给我看看,我的邮箱是[email protected] 希望路过的大虾们多多指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货