线程中如何使用日期时间函数如TIME(100分)

  • 主题发起人 主题发起人 denjin98
  • 开始时间 开始时间
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 Unit2;
interface
uses
Classes, SysUtils;
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.
 
更正:
newtime,oldtime:integer;
应为:
newtime,oldtime:Tdatatime;
 
在你的Unit2中,已经将Time定义为变量了:
var
time,newtime,oldtime:Tdatetime;
 
其实我是用
var
time,newtime,oldtime:Tdatetime;
只不过这里写错了,即使这样也会出现TIME未定义的错误,其实按我的源程序也很容易试试的,请各位给予有建设性的答复!
 
time是时间函数,你不要用time作变量名呀,改成其它的变量名。
 
变量与函数重名了,你可以这么写
oldtime:=SysUtils.Time;
不过你这里只是判断一下时间,为什么不用GetTickCount,不是更好吗?
OldTick:=GetTickCount;
while GetTickCount-OldTick<500do
...
 
感谢各位,感谢LeeChange:
我在Unit2 uses 中加入 SysUtils;,程序可以运行了,但是CUP使用率还是100%,我想解
决的问题还没根本解决,本人是线程方面的新手,恳请各位继续帮助。谢谢!!!
 
可以在循环中加入
Application.ProcessMessage;
也可以加入Sleep适当延时
 
你的过程
procedure hzpmd.addmemo();

while 1=1do
是死循环!
 
你对VCL线程完全误解了。你的线程事实上没起到并行执行的目的。Synchronize的实现方法是把一段代码挂在主线程里执行,你的Execute中做的唯一一件事就是Synchronize!
这个问题不需要线程解决,只需要用Sleep和Application.ProcessMessages就行了。
 
完全没有在使用线程, 你应该只有在访问VCL对象时才synchronize调用, 计算和处理都在线程中处理, 多好啊. 我想你应该修改addmemo, 并把里面的代码放到Excute中来.
 
谢谢各位!!
 
后退
顶部