delphi中的线程程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi中的线程程序相关的知识,希望对你有一定的参考价值。
unit Unit2;
interface
uses
classes;
type
ttest=class(tthread)
private
answer:integer;
protected
procedure giveanswer;
procedure execute; override;
end;
implementation
uses sysutils,unit1;
procedure ttest.giveanswer;
begin
form1.edit1.text:=inttostr(answer);
end;
procedure ttest.execute;
var i:integer;
begin
freeonterminate:=true;
for i:=1 to 2000 do
begin
if terminated then break; //*************
inc(answer,i);
synchronize(giveanswer);
end;
end;
end.
其中的这段循环程序
for i:=1 to 2000 do
begin
if terminated then break;
inc(answer,i);
synchronize(giveanswer);
end;
if terminated then break; 如果线程终止 循环退出
怎样就是线程终止 我删掉这句话 依旧可以得到结果 那这句话起的什么作用
如果你设置一个定时器,并在定时函数里
ttest.terminated :=true;
则视时间长短,则线程执行到这一句会结束。 参考技术B for i:=1 to 2000 do
begin
if terminated then break;
inc(answer,i);
synchronize(giveanswer);
end;
-->
for i:=1 to 200000 do
begin
if terminated then break;
inc(answer,i);
synchronize(giveanswer);
Sleep(10);
end;
就知道了做用了
delphi 7 做多线程程序,内存不断增加,怎么解决?
type
Tmythread = class(TThread)
private
Private declarations
protected
procedure Execute; override;
end;
procedure Tmythread.Execute;
var
s1,s2,ReadStr,sendstr:string; //ReadStr,
j,i,m,n:integer;
begin
readstr:=ReadS;
ReadS:='';
FreeOnTerminate:=true;
EnterCriticalSection(cs);
try
while trim(ReadStr)<>'' do
begin
i:=length(ReadStr);
s1:=leftstr(ReadStr,1);
j:=pos(';',ReadStr);
if (j>0) then
begin
s2:=leftstr(ReadStr,j);
case TypeList.IndexOf(s1) of
0:begin
Frm_ISMain.ReadSearch(s2);
end;
1:begin
//Frm_ISMain.ReadData(s2);
end;
2:begin
//Frm_ISMain.PowerChange(s2);
end;
else
break;
end;//case
if j=i then
ReadStr:=''
else if j<i then
ReadStr:=Rightstr(ReadStr,i-j);
end
else
break; //if
end; //while
Finally
LeaveCriticalSection(CS);
end;
end;
把多线程改成函数,内存就不曾加了,这是怎么回事
参考技术B 用freeandnil(),不用free试一试以上是关于delphi中的线程程序的主要内容,如果未能解决你的问题,请参考以下文章