时间: 2021-07-31 作者:daque
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.