(*----下面这个程序介绍了我们在使用线程及未使用线程二种情况下,运行该程序的反
应。当点usedthread按钮时,则建立一个线程,这时我们可以在程序进行计算的同
时,改变窗体的尺寸及移动它。当按下nousedthread按钮时,不建立线程,我们会发
现在程序没有计算完之前根本不能做其它任何事情!
unit unit1;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls, buttons;
type
tform1 = class(tform)
usedthread: tbitbtn;
nousedthread: tbitbtn;
procedure usedthreadclick(sender: tobject);
procedure nousedthreadclick(sender: tobject);
private
{ private declarations }
public
{ public declarations }
end;
var
form1: tform1;
implementation
{$r *.dfm}
function mythreadfunc(p:pointer):longint;stdcall;
var
i:longint;
dc:hdc;
s:string;
begin
dc:=getdc(form1.handle);
for i:=0 to 500000 do begin
s:=inttostr(i);
textout(dc,10,10,pchar(s),length(s));
end;
releasedc(form1.handle,dc);
end;
procedure tform1.usedthreadclick(sender: tobject);
var
hthread:thandle;//定义一个句柄
threadid:dword;
begin
//创建线程,同时线程函数被调用
hthread:=createthread(nil,0,@mythreadfunc,nil,0,threadid);
if hthread=0 then
messagebox(handle,'didn’tcreateathread',nil,mb_ok);
end;
procedure tform1.nousedthreadclick(sender: tobject);
begin
mythreadfunc(nil);
//没有创建线程时,直接调用线程函数
end;
end.