D
denjin98
Unregistered / Unconfirmed
GUEST, unregistred user!
我写一个显示公告的程序,每隔一定时间(如:(newtime-oldtime)<0.0005)显示一屏,由于CPU一直100%,我改为线程来做,但又无法使用TIME函数,显示TIME未定义,该怎办?
附代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls,unit3;
type
TForm1 = class(TForm)
Button1: TButton;
RichEdit1: TRichEdit;
ComboBox1: TComboBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
t1:hzpmd;
{ Public declarations }
end;
var
Form1: TForm1;
qui: integer;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
t1.Resume;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
t1:=hzpmd.Create(true);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
t1.Suspend;
end;
end.
unit Unit2;
interface
uses
Classes;
type
hzpmd = class(TThread)
private
procedure addmemo();
procedure waiter();
{ Private declarations }
protected
procedure Execute;
override;
end;
var
time,newtime,oldtime:Tdatetime;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure hzpmd.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ hzpmd }
procedure hzpmd.waiter();
var
newtime,oldtime:integer;
begin
oldtime:=time;
newtime:=time;
while (newtime-oldtime)<0.0005do
begin
newtime:=time;
end;
end;
procedure hzpmd.Execute;
begin
synchronize(addmemo);
{ Place thread code here }
end;
procedure hzpmd.addmemo();
var
l, k,j,i,con:integer;
begin
while 1=1do
begin
form1.ComboBox1.Clear;
form1.ComboBox1.Items.LoadFromFile('h:/xxb/zhxk/qsxx.txt');
for k:=1 to 3do
begin
con:=form1.ComboBox1.Items.Count;
for i:=0 to con-1 do
begin
try
form1.richedit1.Lines.Add(form1.ComboBox1.Items);
while form1.richedit1.Lines.Count>=15do
begin
form1.RichEdit1.Refresh;
waiter();
form1.richedit1.Clear;
end;
except
continue;
end;
end;
end;
end;
end;
end.
附代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls,unit3;
type
TForm1 = class(TForm)
Button1: TButton;
RichEdit1: TRichEdit;
ComboBox1: TComboBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
t1:hzpmd;
{ Public declarations }
end;
var
Form1: TForm1;
qui: integer;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
t1.Resume;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
t1:=hzpmd.Create(true);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
t1.Suspend;
end;
end.
unit Unit2;
interface
uses
Classes;
type
hzpmd = class(TThread)
private
procedure addmemo();
procedure waiter();
{ Private declarations }
protected
procedure Execute;
override;
end;
var
time,newtime,oldtime:Tdatetime;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure hzpmd.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ hzpmd }
procedure hzpmd.waiter();
var
newtime,oldtime:integer;
begin
oldtime:=time;
newtime:=time;
while (newtime-oldtime)<0.0005do
begin
newtime:=time;
end;
end;
procedure hzpmd.Execute;
begin
synchronize(addmemo);
{ Place thread code here }
end;
procedure hzpmd.addmemo();
var
l, k,j,i,con:integer;
begin
while 1=1do
begin
form1.ComboBox1.Clear;
form1.ComboBox1.Items.LoadFromFile('h:/xxb/zhxk/qsxx.txt');
for k:=1 to 3do
begin
con:=form1.ComboBox1.Items.Count;
for i:=0 to con-1 do
begin
try
form1.richedit1.Lines.Add(form1.ComboBox1.Items);
while form1.richedit1.Lines.Count>=15do
begin
form1.RichEdit1.Refresh;
waiter();
form1.richedit1.Clear;
end;
except
continue;
end;
end;
end;
end;
end;
end.