主题:Timer控件为什么计时不准确?
我在使用Borland C++ Builder 6.0时,发现一件很奇怪的事:Timer的Interval属性,如果设成200、50、40、10,都很正常,与电脑系统时钟均保持一致。但如果设成25,却发现,Timer1Timer事件实际上并不是每隔25毫秒发生一次,而是(与系统时钟相比)略大于25毫秒。这是怎么回事呢?
我编的小程序如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int times;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
times=0;
Timer1->Interval=25;
if(Timer1->Enabled==true) Timer1->Enabled=false;
else Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
times++;
Label1->Caption=FloatToStr(float(times)*Timer1->Interval/1000);
}
//---------------------------------------------------------------------------
这个程序运行时,我发现,Label1显示的时间,比系统时钟要慢一些。当系统时钟(从0秒开始计数)显示30秒时,Label1(从0秒开始计数)显示25秒。
真怪啊!
有的仁兄告诉我,Timer控件本身,它就不是准确计时的控件。请问:那该怎么办呢?要想准确计时,有什么办法可以实现呢?
我编的小程序如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int times;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
times=0;
Timer1->Interval=25;
if(Timer1->Enabled==true) Timer1->Enabled=false;
else Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
times++;
Label1->Caption=FloatToStr(float(times)*Timer1->Interval/1000);
}
//---------------------------------------------------------------------------
这个程序运行时,我发现,Label1显示的时间,比系统时钟要慢一些。当系统时钟(从0秒开始计数)显示30秒时,Label1(从0秒开始计数)显示25秒。
真怪啊!
有的仁兄告诉我,Timer控件本身,它就不是准确计时的控件。请问:那该怎么办呢?要想准确计时,有什么办法可以实现呢?