我已经实现了这个功能,线程类的代码如下:
unit UnitThread;
interface
uses
Classes,ComObj,Activex,ADODB,UnitPub,Messages,Windows,Sysutils,UnitData;
type
tMyThread = class(TThread)
private
{ Private declarations }
fKey:string;
protected
procedure Execute;
override;
procedure PutData;
public
ADOConn:TADOConnection;
ADQ:TADOQUery;
end;
implementation
{ 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 tMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ tMyThread }
procedure tMyThread.Execute;
var
aMsg:TagMsg;
akey,bkey:string;
begin
OleInitialize(nil);
ADOConn := TADOConnection.Create(nil);
ADOConn.LoginPrompt := False;
ADOConn.ConnectionString := 'Provider=SQLOLEDB.1;Password=4rt5e6;Persist Security Info=True;User ID=sa;Initial Catalog=indexdb;Data Source=192.168.10.237';
ADQ := TADOQuery.Create(nil);
ADQ.Connection := ADOConn;
while GetMessage(aMsg,0,0,0)do
begin
if Terminated then
Break;
if aMsg.message = WM_MYMessage then
begin
WaitForSingleObject(hMutex,INFINITE);
aKey := key;
ReleaseMutex(hMutex);
if (aKey = '') or (aKey = fKey) then
Continue;
fkey := akey;
ADQ.Close;
ADQ.SQL.Text := 'select * from wfyw where qcode = ' + QuotedStr(akey);
ADQ.Open;
Sleep(300);
WaitForSingleObject(hMutex,INFINITE);
bKey := key;
if akey = bkey then
begin
Synchronize(PutData);
end;
ReleaseMutex(hMutex);
end;
end;
ADOConn.Free;
ADQ.Free;
OleUninitialize;
end;
procedure tMyThread.PutData;
begin
if not MyData.YWQR.ControlsDisabled then
mydata.YWQR.DisableControls;
MyData.YWQR.Close;
MyData.YWQR.Clone(ADQ);
if MyData.YWQR.ControlsDisabled then
Mydata.YWQR.EnableControls;
end;
end.