unit BncThrd;interfaceuses Classes,WinProcs,Graphics,ExtCtrls;type TBounceThread = class(TThread) private { Private declarations } FShape:Tshape; FXSpeed:integer; FYSpeed:integer; procedure MoveShape; protected procedure Execute; override; public constructor Create(Suspended:Boolean;Shape:TShape;XSpeed,YSpeed:integer); property Shape:TShape Read FShape; end;implementation{ Important: Methods and properties of objects in VCL can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure BncThrd.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ BncThrd } procedure TBounceThread.MoveShape; var MaxHeight,MaxWidth:Integer; begin with FShape do begin Left:=left+FXSpeed; Top:=top+FYSpeed; if (left<0) or ((Left+width)>Parent.Width) then FxSpeed:=FxSpeed*-1; if (Top<0) or ((Top+Height)>Parent.Height) then FYSpeed:=FySpeed*-1; end;end; procedure TBounceThread.Execute; begin while not Terminated do begin Synchronize(MoveShape); end; { Place thread code here } end;Constructor TBounceThread.Create(Suspended:Boolean;Shape:TShape;XSpeed,YSpeed:integer); begin Inherited Create(Suspended); FShape:=Shape; FXspeed:=Xspeed; FYSpeed:=YSpeed; FreeOnTerminate:=True; end;end. unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Button1: TButton; Shape1: TShape; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation uses BncThrd; {$R *.DFM}procedure TForm1.Button1Click(Sender: TObject); begin TBounceThread.Create(false,Shape1,1,2) end;end.
这是一个很简单的例子。你可以看一看,我想会对你有很多帮助的。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) myedit: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; tmythread = class(tthread) count:integer; myedit:tedit; procedure show;virtual;abstract; constructor create(myedit1:tedit); end; thread1 = class(tmythread) procedure show;override; procedure execute;override; end; thread2 = class(tmythread) procedure show;override; procedure execute;override; end; var Form1: TForm1;implementationprocedure mythreadfunc; var i:integer; dc:hdc; s:string; begin for i:=0 to 1000000 do begin s:=inttostr(i); dc:=getdc(form1.myedit.handle); textout(dc,0,0,pchar(s),length(s)); releasedc(form1.myedit.handle,dc); end; end;constructor tmythread.create(myedit1:tedit); begin inherited create(false); myedit:=myedit1; freeonterminate:=true; end;procedure thread1.show; begin myedit.Text:=inttostr(count); end;procedure thread1.execute; var i:integer; begin for i:=0 to 1000000 do begin count:=i; synchronize(show); end; end;procedure thread2.show; begin mythreadfunc; end;procedure thread2.execute; begin synchronize(show); end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin with thread1.create(myedit) do end;procedure TForm1.Button2Click(Sender: TObject); begin with thread2.create(myedit) do end;procedure TForm1.Button3Click(Sender: TObject); var hthread:thandle; thid:dword; begin hthread:=beginthread(nil,0,@mythreadfunc,nil,0,thid); if hthread=0 then showmessage('failed'); end;procedure TForm1.Button4Click(Sender: TObject); begin mythreadfunc; end;end.
TClientDataThread = class(TThread) private public ListBuffer :TStringList; TargetList :TStrings; procedure synchAddDataToControl; constructor Create(CreateSuspended: Boolean); procedure Execute; override; procedure Terminate; end; constructor TClientDataThread.Create(CreateSuspended: Boolean); begin inherited Create(CreateSuspended); FreeOnTerminate := true; ListBuffer := TStringList.Create; end;procedure TClientDataThread.Terminate; begin ListBuffer.Free; inherited; end;procedure TClientDataThread.Execute; begin freeonterminate:=true; Synchronize(synchAddDataToControl); end;procedure TClientDataThread.synchAddDataToControl; begin TargetList.AddStrings(ListBuffer); end;
unit Unit3;interface uses SysUtils, Classes, Controls, Forms, ADODB,ActiveX;type TDBQyeryThread=Class(TThread) private FQuery:TADOQuery; FSqlStr:String; FFlag:Boolean; FQueryException:Exception; procedure QueryError; protected procedure Execute;override; public constructor Create(Q:TADOQuery;Sql:String;Flag:Boolean);virtual; end;implementation constructor TDBQyeryThread.Create(Q:TADOQuery;Sql:String;Flag:Boolean); begin inherited Create(Flag); FQuery:=Q; FSqlStr:=Sql; FFlag:=False; {FreeOnTerminate:=True; Resume; } end;procedure TDBQyeryThread.Execute; begin CoInitialize(Nil); sleep(1000); try FQuery.Close; FQuery.SQL.Clear; FQuery.SQL.Add(FSqlStr); FQuery.Open; except FQueryException:=ExceptObject as Exception; synchronize(QueryError); end; CoUninitialize; end;procedure TDBQyeryThread.QueryError; begin Application.ShowException(FQueryException); end; end.procedure TForm1.Button1Click(Sender: TObject); var SThread:TDBShowThread; begin //QThread.Create(MaterialQuery); SThread:=TDBShowThread.Create(False); //SThread(MaterialQuery,SqlStr,True); SThread.Resume; end;
Classes,WinProcs,Graphics,ExtCtrls;type
TBounceThread = class(TThread)
private
{ Private declarations }
FShape:Tshape;
FXSpeed:integer;
FYSpeed:integer;
procedure MoveShape;
protected
procedure Execute; override;
public
constructor Create(Suspended:Boolean;Shape:TShape;XSpeed,YSpeed:integer);
property Shape:TShape Read FShape;
end;implementation{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure BncThrd.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ BncThrd }
procedure TBounceThread.MoveShape;
var
MaxHeight,MaxWidth:Integer;
begin
with FShape do
begin
Left:=left+FXSpeed;
Top:=top+FYSpeed;
if (left<0) or ((Left+width)>Parent.Width) then
FxSpeed:=FxSpeed*-1;
if (Top<0) or ((Top+Height)>Parent.Height) then
FYSpeed:=FySpeed*-1; end;end;
procedure TBounceThread.Execute;
begin
while not Terminated do
begin
Synchronize(MoveShape);
end;
{ Place thread code here }
end;Constructor TBounceThread.Create(Suspended:Boolean;Shape:TShape;XSpeed,YSpeed:integer);
begin
Inherited Create(Suspended);
FShape:=Shape;
FXspeed:=Xspeed;
FYSpeed:=YSpeed;
FreeOnTerminate:=True;
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Shape1: TShape;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
uses BncThrd;
{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
TBounceThread.Create(false,Shape1,1,2)
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
myedit: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
tmythread = class(tthread)
count:integer;
myedit:tedit;
procedure show;virtual;abstract;
constructor create(myedit1:tedit);
end; thread1 = class(tmythread)
procedure show;override;
procedure execute;override;
end; thread2 = class(tmythread)
procedure show;override;
procedure execute;override;
end;
var
Form1: TForm1;implementationprocedure mythreadfunc;
var
i:integer;
dc:hdc;
s:string;
begin
for i:=0 to 1000000 do
begin
s:=inttostr(i);
dc:=getdc(form1.myedit.handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.myedit.handle,dc);
end;
end;constructor tmythread.create(myedit1:tedit);
begin
inherited create(false);
myedit:=myedit1;
freeonterminate:=true;
end;procedure thread1.show;
begin
myedit.Text:=inttostr(count);
end;procedure thread1.execute;
var
i:integer;
begin
for i:=0 to 1000000 do
begin
count:=i;
synchronize(show);
end;
end;procedure thread2.show;
begin
mythreadfunc;
end;procedure thread2.execute;
begin
synchronize(show);
end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
with thread1.create(myedit) do
end;procedure TForm1.Button2Click(Sender: TObject);
begin
with thread2.create(myedit) do
end;procedure TForm1.Button3Click(Sender: TObject);
var
hthread:thandle;
thid:dword;
begin
hthread:=beginthread(nil,0,@mythreadfunc,nil,0,thid);
if hthread=0 then
showmessage('failed');
end;procedure TForm1.Button4Click(Sender: TObject);
begin
mythreadfunc;
end;end.
private
public
ListBuffer :TStringList;
TargetList :TStrings;
procedure synchAddDataToControl;
constructor Create(CreateSuspended: Boolean);
procedure Execute; override;
procedure Terminate;
end;
constructor TClientDataThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
ListBuffer := TStringList.Create;
end;procedure TClientDataThread.Terminate;
begin
ListBuffer.Free;
inherited;
end;procedure TClientDataThread.Execute;
begin
freeonterminate:=true;
Synchronize(synchAddDataToControl);
end;procedure TClientDataThread.synchAddDataToControl;
begin
TargetList.AddStrings(ListBuffer);
end;
uses SysUtils, Classes, Controls, Forms, ADODB,ActiveX;type
TDBQyeryThread=Class(TThread)
private
FQuery:TADOQuery;
FSqlStr:String;
FFlag:Boolean;
FQueryException:Exception;
procedure QueryError;
protected
procedure Execute;override;
public
constructor Create(Q:TADOQuery;Sql:String;Flag:Boolean);virtual;
end;implementation
constructor TDBQyeryThread.Create(Q:TADOQuery;Sql:String;Flag:Boolean);
begin
inherited Create(Flag);
FQuery:=Q;
FSqlStr:=Sql;
FFlag:=False;
{FreeOnTerminate:=True;
Resume; }
end;procedure TDBQyeryThread.Execute;
begin
CoInitialize(Nil);
sleep(1000);
try
FQuery.Close;
FQuery.SQL.Clear;
FQuery.SQL.Add(FSqlStr);
FQuery.Open; except
FQueryException:=ExceptObject as Exception;
synchronize(QueryError);
end;
CoUninitialize;
end;procedure TDBQyeryThread.QueryError;
begin
Application.ShowException(FQueryException);
end;
end.procedure TForm1.Button1Click(Sender: TObject);
var
SThread:TDBShowThread;
begin
//QThread.Create(MaterialQuery);
SThread:=TDBShowThread.Create(False);
//SThread(MaterialQuery,SqlStr,True);
SThread.Resume;
end;