unit Uthreadqry;
interface
uses
Classes, ActiveX, ADODB;
type
TThQry = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
implementation
uses
threadqryFrm;
{ 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 TThQry.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TThQry }
procedure TThQry.Execute;
var
conn: TADOConnection;
qry:TADOQuery;
begin
{ Place thread code here }
// This is true.
//// CoInitialize(nil);
//// Conn:=TADOConnection.Create(nil);
//// Conn.LoginPrompt:=False;
//// qry:=TADOQuery.Create(nil);
////
//// Conn.ConnectionString:=Form1.ADOConnection1.ConnectionString;
//// qry.Close;
//// qry.SQL.Text := 'SELECT * FROM KC';
//// qry.Connection:=Conn;
//// Form1.DataSource1.DataSet:=qry;
//// qry.Open;
//// Couninitialize;
Coinitialize(nil);
Form1.ADOQuery1.Close;
Form1.ADOQuery1.SQL.Text :='SELECT * FROM KC';
Form1.ADOQuery1.Open;
Couninitialize;
end;
end.
unit threadqryFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure threaddone(sender: TObject);
procedure thover;
end;
var
Form1: TForm1;
implementation
uses
Uthreadqry;
var
th: TThQry;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
th :=TThQry.Create(True);
th.FreeOnTerminate:=True;
th.OnTerminate :=threaddone;
th.Resume;
end;
procedure TForm1.thover;
begin
th:=nil;
end;
procedure TForm1.threaddone(sender: TObject);
begin
th:=nil;
end;
end.
这个程序是可以正确的,但是我前面写的却不能正确。