求一段循环代码,不用Timer控件! 如何循环每秒去执行某个事件;类似Timer控件功能;但是要比Timer控件的时间精度高!求代码! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这样说的话,那么就用一个线程吧,类似下面的unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SyncObjs;type TTimeControl = class(TThread) private FTimeRange: Integer; //毫秒 FEvent: TSimpleEvent; protected procedure Execute; override; procedure DoSomeThing; public constructor create(const ATimeRange: Integer); destructor destroy; override; end; TForm1 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } FTimeControl: TTimeControl; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}{ TTimeControl }constructor TTimeControl.create(const ATimeRange: Integer);begin FTimeRange := ATimeRange; FEvent := TSimpleEvent.Create; FreeOnTerminate := False; inherited Create(False);end;destructor TTimeControl.destroy;begin Terminate; if Assigned(FEvent) then FEvent.SetEvent; FreeAndNil(FEvent); inherited;end;procedure TTimeControl.DoSomeThing;begin ;end;procedure TTimeControl.Execute;begin while not Terminated do begin if FEvent.WaitFor(FTimeRange) <> wrTimeOut then begin Terminate; Break; end else DoSomeThing; end; inherited;end;procedure TForm1.btn1Click(Sender: TObject);begin FTimeControl := TTimeControl.create(300); //300毫秒 FTimeControl.Resume;end;procedure TForm1.FormDestroy(Sender: TObject);begin FreeAndNil(FTimeControl);end;end. 本帖最后由 bdmh 于 2011-08-08 08:26:07 编辑 谢谢楼上的2位大神!还是CSDN好啊! 用多媒体定时器最精确了包含mmsystemtimeSetEvent 谁帮写个全局hook,屏蔽按键消息和鼠标消息,热键退出? 下面这两种写法在inherited的效果上有什么区别么? 也提问一个关于Access数据更新的奇怪问题,谢谢 在线给分 bgrid列显示的问题 Delphi是否有上升的空间,答者有分!!!新年快乐!!!! 请问Oracle的10用户、5用户是怎么回事? 谁能帮我把这段代码注释下下 !!! INDY10里如何使用带SSL的TIdHttpServer,INDY9的调用不适合在INDY10 如何实现在每页中显示报表总页数 在delphi的数据库控件中的(是不是叫有个dbgrid的,我都快忘了)的数据列的定位是怎么实现的? Delphi7调用Java的Web Service出错 Delphi 定时清理内存的方法?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SyncObjs;type
TTimeControl = class(TThread)
private
FTimeRange: Integer; //毫秒
FEvent: TSimpleEvent;
protected
procedure Execute; override;
procedure DoSomeThing;
public
constructor create(const ATimeRange: Integer);
destructor destroy; override;
end; TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FTimeControl: TTimeControl;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TTimeControl }constructor TTimeControl.create(const ATimeRange: Integer);
begin
FTimeRange := ATimeRange;
FEvent := TSimpleEvent.Create;
FreeOnTerminate := False;
inherited Create(False);
end;destructor TTimeControl.destroy;
begin
Terminate;
if Assigned(FEvent) then
FEvent.SetEvent;
FreeAndNil(FEvent);
inherited;
end;procedure TTimeControl.DoSomeThing;
begin
;
end;procedure TTimeControl.Execute;
begin
while not Terminated do
begin
if FEvent.WaitFor(FTimeRange) <> wrTimeOut then
begin
Terminate;
Break;
end
else
DoSomeThing;
end;
inherited;
end;procedure TForm1.btn1Click(Sender: TObject);
begin
FTimeControl := TTimeControl.create(300); //300毫秒
FTimeControl.Resume;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(FTimeControl);
end;end.
timeSetEvent