这里就用到了前面说到的delphi rtl函数beginthread,它有很多参数,关键的是第三、四两个参数。第三个参数就是前面说到的线程函数,即在线程中执行的代码部分。第四个参数则是传递给线程函数的参数,在这里就是创建的线程对象(即self)。其它的参数中,第五个是用于设置线程在创建后即挂起,不立即执行(启动线程的工作是在afterconstruction中根据createsuspended标志来决定的),第六个是返回线程id。
现在来看tthread的核心:线程函数threadproc。有意思的是这个线程类的核心却不是线程的成员,而是一个全局函数(因为beginthread过程的参数约定只能用全局函数)。下面是它的代码:
function threadproc(thread: tthread): integer;
var
freethread: boolean;
begin
try
if not thread.terminated then
try
thread.execute;
except
thread.ffatalexception := acquireexceptionobject;
end;
finally