熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

Delphi中三種延時方法及其定時精度分析

2013-11-23 17:29:18  來源: Delphi編程 
在Delphi中通常可以用以下三種方法來實現程序的延時即TTtimer控件Sleep函數GetTickCount函數但是其精度是各不相同的
  
  一三種方法的簡單介紹
  
  )TTtimer控件
  
  TTtimer控件的實質是調用Windows API定時函數SetTimer和KillTimer來實現的並簡化了對WM_TIMER 消息的處理過程通過設置OnTimer事件和Interval屬性我們可以很方便的產生一些簡單的定時事件
  
  )Sleep函數
  
  Sleep函數用來使程序的執行延時給定的時間值Sleep的調用形式為Sleep(milliseconds)暫停當前的進程milliseconds毫秒Sleep的實現方法其實也是調用Windows API的Sleep函數例如
  
  sleep();    //延遲毫秒
  
  Sleep會引起程序停滯如果你延遲的時間較長的話你的程序將不能夠響應延時期間的發生的其他消息所以程序看起來好像暫時死機
  
  )GetTickCount函數
  
  在主程序中延時為了達到延時和響應消息這兩個目的GetTickCount()構成的循環就是一種廣為流傳的方法例如
  
  procedure Delay(MSecs: Longint);
  //延時函數MSecs單位為毫秒(千分之秒)
  var
  FirstTickCount Now: Longint;
  begin
  FirstTickCount := GetTickCount();
  repeat
  ApplicationProcessMessages;
  Now := GetTickCount();
  until (Now FirstTickCount >= MSecs) or (Now < FirstTickCount);
  end;
  
  二高精度的微妙級性能計數器(highresolution performance counter)介紹
  
  為了比較以上方法的精度首先需要找到一個參考的定時器在這裡我提供了兩個參考的定時器一是用單片機每隔ms產生一個實時中斷RTI作為計數器二是選用了一個高精度的微妙級性能計數器(參見 ltaspx 或者
  )
  
  )計數器的Delphi源代碼
  
  {
  A highprecision counter/timer Retrieves time differences
  downto microsec
  Quick Reference:
  THPCounter inherits from TComponent
  
  KeyMethods:
  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 Int 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
  TInt = TLargeInteger;
  THPCounter = class(TComponent)
  private
  Frequency: TLargeInteger;
  lpPerformanceCount: TLargeInteger;
  lpPerformanceCount: 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 THPCounterCreate(AOwner: TComponent);
  begin
  inherited Create(AOwner);
  fAbout:= Version  ® Mats Asplund EMail:  Site: ;
  end;
  
  destructor THPCounterDestroy;
  begin
  inherited Destroy;
  end;
  
  function THPCounterRead: string;
  begin
  QueryPerformanceCounter(TInt((@lpPerformanceCount)^));
  QueryPerformanceFrequency(TInt((@Frequency)^));
  Result:=IntToStr(Round( * (lpPerformanceCount 
  lpPerformanceCount) / Frequency));
  end;
  
  function THPCounterReadInt: TLargeInteger;
  begin
  QueryPerformanceCounter(TInt((@lpPerformanceCount)^));
  QueryPerformanceFrequency(TInt((@Frequency)^));
  Result:=Round( * (lpPerformanceCount 
  lpPerformanceCount) / Frequency);
  end;
  
  procedure THPCounterSetAbout(Value: string);
  begin
  Exit;
  end;
  
  procedure THPCounterStart;
  begin
  QueryPerformanceCounter(TInt((@lpPerformanceCount)^));
  end;
  
  end
  
  )使用方法
  unit Unit;
  
  interface
  
  uses
  Windows Messages SysUtils Classes Graphics Controls Forms Dialogs
  HPCounter StdCtrls;
  
  type
  TForm = class(TForm)
  Button: TButton;
  Edit: TEdit;
  Label: TLabel;
  Label: TLabel;
  procedure ButtonClick(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;
  
  var
  Form: TForm;
  
  implementation
  
  {$R *DFM}
  
  procedure TFormButtonClick(Sender: TObject);
  begin
  EditText:= ;
  ApplicationProcessMessages;
  with THPCounterCreate(Self) do
  begin
  Start;
  // Place code to measure here
  Sleep();
  // Place code to measure here
  EditText:=Read;
  Free;
  end;
  end;
  
  end
  
  三三種方法的精度比較
  
  為了比較采用以上種方法分別設置延時時間為msmsmsmsmsmsmsmsmsms循環次數為得到實際的延時時間
  
  )TTtimer控件
  
  實際延時時間(ms)
  ms       
  ms      
  ms       
  ms      
  ms    
  ms    
  ms
  ms
  ms
  ms
  
  )Sleep函數
  
  ms        
  ms        
  ms          
  ms    
  ms    
  ms      
  ms
  ms
  ms
  ms
  
  )GetTickCount函數
  
  ms      
  ms     
  ms     
  ms    
  ms     
  ms     
  ms  
  ms
  ms
  ms
  
  可見相對而言Sleep的精度最高尤其是在ms以內的延時只有sleep函數才能夠做到TTimer控件的定時精度最差而且穩定性不好波動很大GetTickCount函數所能實現的最短延時為ms左右穩定性相對TTimer要好一些
From:http://tw.wingwit.com/Article/program/Delphi/201311/24673.html
  • 上一篇文章:

  • 下一篇文章:
  • Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.