线程代码:unit untLinktoRemoteDBS;interfaceuses Classes,ExtCtrls,SysUtils;type tLinktoRemoteDBS = class(TThread) private { Private declarations } fdepart:integer; protected procedure Execute; override; public constructor Create(idepart:integer); end;implementation{ Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure LinktoRemoteDBS.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ LinktoRemoteDBS } uses frmmain,untpublic;procedure tLinktoRemoteDBS.Execute; var oImg:timage; begin with tdepartments[fdepart-1] do begin if (not adocConn.connected) and (adocConn.ConnectionString<>'') then begin try adocConn.open; showmsg('连接服务器'+trim(departName)+'成功',informationset); except on E:Exception do begin showmsg('连接服务器'+trim(departName)+'出错',informationset); Terminate; end; end; end; end;end; constructor tLinktoRemoteDBS.Create(idepart:integer); begin fDepart:=idepart; inherited Create(False); end;end. 调用代码: for i:=1 to length(tdepartments) do begin tlinktoremotedbs(i); end;
Classes,ExtCtrls,SysUtils;type
tLinktoRemoteDBS = class(TThread)
private
{ Private declarations }
fdepart:integer;
protected
procedure Execute; override;
public
constructor Create(idepart:integer);
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure LinktoRemoteDBS.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ LinktoRemoteDBS }
uses frmmain,untpublic;procedure tLinktoRemoteDBS.Execute;
var oImg:timage;
begin
with tdepartments[fdepart-1] do
begin
if (not adocConn.connected) and (adocConn.ConnectionString<>'') then
begin
try
adocConn.open;
showmsg('连接服务器'+trim(departName)+'成功',informationset);
except
on E:Exception do begin
showmsg('连接服务器'+trim(departName)+'出错',informationset);
Terminate;
end;
end;
end;
end;end;
constructor tLinktoRemoteDBS.Create(idepart:integer);
begin
fDepart:=idepart;
inherited Create(False);
end;end.
调用代码:
for i:=1 to length(tdepartments) do
begin
tlinktoremotedbs(i);
end;
在多线程里引用外面的FORM,最好是作参数传入。