{ Important: Methods and properties of objects in VCL or CLX can only be used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Ithreads.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }
{ Ithreads } uses Unit1;
procedure Ithreads.Giveanswer ; begin Form1.Edit1.Text := IntToStr(awer); end;
procedure Ithreads.Execute; var i:integer; begin { Place thread code here } for i:= 1 to 500000 do begin inc(awer,round(abs(sin(sqrt(i))))); synchronize(Giveanswer); end; end;
{$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.Button1Click(Sender: TObject); begin //创建线程,同时线程函数被调用 hthread:=CreateThread(nil,0,@MyThreadfunc,nil,0,ThreadID); end;
procedure TForm1.Button2Click(Sender: TObject); begin SuspendThread(hThread); //挂起线程 end;
procedure TForm1.Button3Click(Sender: TObject); begin ResumeThread(hThread); // 激活线程 end;
procedure TForm1.Button4Click(Sender: TObject); begin TerminateThread(hThread,0); // 终止线程 end;