如何控制线程的执行和挂起?(100分)

好的,我先试试,不行的话还要请大家帮忙!!
 
在Execute中必须使用while not terminateddo
...这样的循环(除非你的代码只需要在线程中运行一次)。因为随着TThread.Execute的结束整个线程也结束了。
 
unit Umythread;
interface
uses
Classes,db,StdCtrls,SysUtils,udatamodule1,Graphics,Math,Buttons;
//自定义线程,对通信接受的数据进行处理
type
tmythread = class(TThread)
proceduredo
comm;
//数据处理过程
private{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create (Suspended:Boolean);
end;

implementation

constructor tmythread.Create(Suspended:Boolean);
begin
inherited Create(Suspended);
FreeOnTerminate:=true;
end;


procedure tmythread.Execute;
//线程执行过程
begin
//1
while not Terminated do
begin

do
comm;

sleep(5);
end;
//1
end;

procedure tmythread.docomm;
begin
//处理
end;
end;

end.
代码:
再在主线程中声明线程对象
var
mythread:tmythread;
然后执行
mythread:=tmythread.create(false);
最后不用时释放
mythread.Terminate;

 
Another_eYes:我知道你说的,但是现在是,我想让这个线程每隔几秒执行一次,该怎么办呢?
我现在想用线程缓存,不知道可不可以实现?大家给个建议啊!!!
 
关注,收藏,学习!!!!!!!!!!!!!!!!!!!!!!!!
 
使用其他的通信机制,比如Event,信号量等等,这样可以在逻辑上分得很清,在需要的时候置一下标志,在线程内部合适的地方检测这个标志,以及等待这个标志
TMyThread.Execute
begin
while not Terminateddo
begin
do
Something;
WaitForSingleObject();
end;
end;
 
先写好线程执行的函数,然后在timer事件当中进行线程的挂起与唤醒,但要注意得事先设置好timer的interval属性,在调用处理事件中最好设置好同步问题这方面我只懂这么多了也帮你顶一下了
 
顶部