unit UTimeDisplay;interfaceuses
Classes, Graphics, ExtCtrls,Controls,SysUtils,Dialogs,StdCtrls;type
TCurrTimeDisplay= Class(TThread)
Private
aTimer:TTimer;
aLabel:TLabel;
protected
procedure Execute; override;
procedure DoTerminate;override;
Public
Constructor Create(var TmShowLbl:TLabel);
procedure TimerTimer(Sender:TObject);
end;implementationConstructor TCurrTimeDisplay.Create(Var TmShowLbl:TLabel);
begin
//Priority:=tpHigher;
FreeOnTerminate:=True;
aLabel:=TmShowLbl;
aTimer:=TTimer.Create(nil);
with aTimer do
begin
Interval:=1000;
OnTimer:=TimerTimer;
end;
inherited Create(False);
end;Procedure TCurrTimeDisplay.Execute;
begin
TimerTimer(aTimer);
end;procedure TCurrTimeDisplay.TimerTimer(Sender:TObject);
begin
aLabel.Caption:=TimeToStr(Time);
aLabel.Refresh;
aTimer.Enabled:=True;
end;Procedure TCurrTimeDisplay.DoTerminate;
begin
if aTimer<>nil then
aTimer.Free;
end;现在问题是使用这个线层的程序 只要lose focus(鼠标点到程序外面)就出现“read voliation 0×00000...”错误怎么控制线程的结束 我不会。同时还有个问题,我这样写主线层上的赋给“Var TmShowLbl:TLabel”的label 并不会一秒一秒变化 而是就第一次执行的时间 :(
Classes, Graphics, ExtCtrls,Controls,SysUtils,Dialogs,StdCtrls;type
TCurrTimeDisplay= Class(TThread)
Private
aTimer:TTimer;
aLabel:TLabel;
protected
procedure Execute; override;
procedure DoTerminate;override;
Public
Constructor Create(var TmShowLbl:TLabel);
procedure TimerTimer(Sender:TObject);
end;implementationConstructor TCurrTimeDisplay.Create(Var TmShowLbl:TLabel);
begin
//Priority:=tpHigher;
FreeOnTerminate:=True;
aLabel:=TmShowLbl;
aTimer:=TTimer.Create(nil);
with aTimer do
begin
Interval:=1000;
OnTimer:=TimerTimer;
end;
inherited Create(False);
end;Procedure TCurrTimeDisplay.Execute;
begin
TimerTimer(aTimer);
end;procedure TCurrTimeDisplay.TimerTimer(Sender:TObject);
begin
aLabel.Caption:=TimeToStr(Time);
aLabel.Refresh;
aTimer.Enabled:=True;
end;Procedure TCurrTimeDisplay.DoTerminate;
begin
if aTimer<>nil then
aTimer.Free;
end;现在问题是使用这个线层的程序 只要lose focus(鼠标点到程序外面)就出现“read voliation 0×00000...”错误怎么控制线程的结束 我不会。同时还有个问题,我这样写主线层上的赋给“Var TmShowLbl:TLabel”的label 并不会一秒一秒变化 而是就第一次执行的时间 :(
解决方案 »
- 怎么控制Paintbox的OnPaint事件的发生顺序?
- quickreport问题
- 登录问题? (if else)
- delphi中关于stringgrid控件的打印
- 菜鸟问题,谢谢了!
- 黑白二色的bitmap转gif,可有人成功过?找了一些算法,都不支持bitcount为1的bitmap.
- 100分求ABC FOR delphi 7
- 主从表分组打印时,是否需要什么规则?
- 该怎么检索屏幕鼠标?
- 100分,用友财务软件那样的记帐凭证录入和报表数据录入的功能用什么控件怎么实现?500分求购例子代码!
- 如何将循环中的变量实时送到另外一个窗口的控件中
- 在delphi环境中实现调用fastReport做的报表:(报表菜鸟急。。。
大的有:
1、
Procedure TCurrTimeDisplay.Execute;
begin
TimerTimer(aTimer); //此句执行完毕后,线程就要销毁了,也就是执行DoTerminate,所以“label 并不会一秒一秒变化 而是就第一次执行的时间”
end;
2、TCurrTimeDisplay.TimerTimer中对共享资源aLabel直接操作,这是不安全的,应该放在TThread.Synchronize中。
其他有:
1、Constructor Create(var TmShowLbl:TLabel);中没必要引用传递TmShowLbl,因为TLabel本身就是指针,宜改为:
Constructor Create(TmShowLbl:TLabel);
2、TCurrTimeDisplay.DoTerminate;中应该集成父类的方法。“怎么控制线程的结束 我不会”,你这个线程根本就不好控制什么时候结束,原因是根本就不应该在线程中使用TTimer。线程结束是在Execute中控制的,如果你一定要采用现在的设计方式,那么应该加一个控制变量,在Execute中判断该变量的值从而控制Timer.Enabled。—————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
—————————————————————————————————