转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Delphi中三种延时方法及其定时精度分析         ★★★★

Delphi中三种延时方法及其定时精度分析

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1947 更新时间:2009/4/23 18:27:02

  在Delphi中,通常可以用以下三种方法来实现程序的延时,即TTtimer控件,Sleep函数,GetTickCount函数。但是其精度是各不相同的。

一、三种方法的简单介绍

1)TTtimer控件

  TTtimer控件的实质是调用Windows API定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER 消息的处理过程。通过设置OnTimer事件和Interval属性,我们可以很方便的产生一些简单的定时事件。

2)Sleep函数

  Sleep函数用来使程序的执行延时给定的时间值。Sleep的调用形式为Sleep(milliseconds),暂停当前的进程milliseconds毫秒。Sleep的实现方法其实也是调用Windows API的Sleep函数。例如:

sleep(1000);        //延迟1000毫秒

Sleep会引起程序停滞,如果你延迟的时间较长的话,你的程序将不能够响应延时期间的发生的其他消息,所以程序看起来好像暂时死机。

3)GetTickCount函数

  在主程序中延时,为了达到延时和响应消息这两个目的,GetTickCount()构成的循环就是一种广为流传的方法。例如:

procedure Delay(MSecs: Longint);
//延时函数,MSecs单位为毫秒(千分之1秒)
var
  FirstTickCount, Now: Longint;
begin
  FirstTickCount := GetTickCount();
  repeat
    Application.ProcessMessages;
    Now := GetTickCount();
  until (Now - FirstTickCount >= MSecs) or (Now < FirstTickCount);
end;

二、高精度的微妙级性能计数器(high-resolution performance counter)介绍

  为了比较以上方法的精度,首先需要找到一个参考的定时器。在这里,我提供了两个参考的定时器。一是用单片机每隔1.024ms产生一个实时中断RTI,作为计数器;二是选用了一个高精度的微妙级性能计数器(参见: http://msdn.microsoft.com/msdnmag/issues/04/03/HighResolutionTimer/default.aspx ,或者 http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=200249

1)计数器的Delphi源代码


A  high-precision  counter/timer.  Retrieves  time  differences 
                               downto  microsec. 
Quick  Reference: 
                               THPCounter  inherits  from  TComponent. 
 
                               Key-Methods: 
                                   Start:        Starts  the  counter.  Place  this  call  just  before  the 
                                                       code  you  want  to  measure. 
 
                                   Read:          Reads  the  counter  as  a  string.  Place  this  call  just 
                                                       after  the  code  you  want  to  measure. 
 
                                   ReadInt:    Reads  the  counter  as  an  Int64.  Place  this  call  just 
                                                       after  the  code  you  want  to  measure. 
-------------------------------------------------------------------------------- 

unit  HPCounter; 
 
interface 
 
uses 
   SysUtils,  WinTypes,  WinProcs,  Messages,  Classes,  Graphics,  Controls, 
   Forms,  Dialogs,  StdCtrls,  ExtCtrls; 
 
type 
   TInt64  =  TLargeInteger; 
   THPCounter  =  class(TComponent) 
private 
   Frequency:  TLargeInteger; 
   lpPerformanceCount1:  TLargeInteger; 
   lpPerformanceCount2:  TLargeInteger; 
   fAbout:  string; 
   procedure  SetAbout(Value:  string); 
   {  Private  declarations  } 
public 
   constructor  Create(AOwner:  TComponent);  override; 
   destructor  Destroy;  override; 
   procedure  Start; 
   function  Read:  string; 
   function  ReadInt:  TLargeInteger; 
   {  Private  declarations  } 
published 
   property  About:  string  read  fAbout  write  SetAbout; 
   {  Published  declarations  } 
end; 
 
 
procedure  Register; 
 
implementation 
 
procedure  Register; 
begin 
   RegisterComponents(''''MAs  Prod.'''',  [THPCounter]); 
end; 
 
constructor  THPCounter.Create(AOwner:  TComponent); 
begin 
   inherited  Create(AOwner); 
   fAbout:=  ''''Version  1.1,  2000&reg;  Mats  Asplund,  EMail:  masprod@telia.com,  Site:  http://go.to/masdp''''; 
end; 
 
destructor  THPCounter.Destroy; 
begin 
   inherited  Destroy; 
end; 
 
function  THPCounter.Read:  string; 
begin 
   QueryPerformanceCounter(TInt64((@lpPerformanceCount2)^)); 
   QueryPerformanceFrequency(TInt64((@Frequency)^)); 
   Result:=IntToStr(Round(1000000  *  (lpPerformanceCount2  - 
                                             lpPerformanceCount1)  /  Frequency)); 
end; 
 
function  THPCounter.ReadInt:  TLargeInteger; 
begin 
   QueryPerformanceCounter(TInt64((@lpPerformanceCount2)^)); 
   QueryPerformanceFrequency(TInt64((@Frequency)^)); 
   Result:=Round(1000000  *  (lpPerformanceCount2  - 
                                             lpPerformanceCount1)  /  Frequency); 
end; 
 
procedure  THPCounter.SetAbout(Value:  string); 
begin 
   Exit; 
end; 
 
procedure  THPCounter.Start; 
begin 
   QueryPerformanceCounter(TInt64((@lpPerformanceCount1)^)); 
end; 
 
end. 

2)使用方法: 
unit  Unit1; 
 
interface 
 
uses 
   Windows,  Messages,  SysUtils,  Classes,  Graphics,  Controls,  Forms,  Dialogs, 
   HPCounter,  StdCtrls; 
 
type 
   TForm1  =  class(TForm) 
       Button1:  TButton; 
       Edit1:  TEdit; 
       Label1:  TLabel; 
       Label2:  TLabel; 
       procedure  Button1Click(Sender:  TObject); 
   private 
       {  Private  declarations  } 
   public 
       {  Public  declarations  } 
   end; 
 
var 
   Form1:  TForm1; 
 
implementation 
 
{$R  *.DFM} 
 
procedure  TForm1.Button1Click(Sender:  TObject); 
begin 
   Edit1.Text:=  ''''''''; 
   Application.ProcessMessages; 
   with  THPCounter.Create(Se

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台