写了个线程,每隔一秒显示一个数字(从0开始,每次加一)。但是发现,有时候会不显示,之后就跳过了这个数字。比如:正常情况下,会从1顺序地显示到8,但是应该显示9的时候,屏幕为空白,之后却显示10了;还有种情况,显示的时候突然为粗体显示,之后又恢复了,我并没有改变过字体字号!
效果图:
我现在把代码全部贴上,请高人帮忙看看错在哪,谢谢!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,DateUtils;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyThread = class(TThread)
private
{ Private declarations }
FOwner: TForm;
FLastAutoCMDTime: TDateTime;
protected
procedure Execute; override;
public
constructor Create(Owner: TForm); virtual;
end;
var
Form1: TForm1;
FThread:TmyThread ;
FLastAutoCMDTime: TDateTime;
fval:longint ;
implementation
{$R *.dfm}
constructor TMyThread.Create(Owner: TForm);
begin
inherited Create(False);
FOwner := Owner;
FLastAutoCMDTime := Now;
fval:=0 ;
end;
procedure TMyThread.Execute;
begin
while not Terminated do
begin
if MilliSecondsBetween(Now, FLastAutoCMDTime) > 1000 then //每隔1秒
begin
FLastAutoCMDTime := Now;
inc(fval) ;
Tform1(FOwner).Label1.Caption:=inttostr(fval) ;
end ;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if FThread<>nil then
begin
FThread.Terminate;
FThread.WaitFor;
FThread.Free;
end ;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FThread:=TMyThread.Create(form1) ;
end;
end.