L
lwbcool
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DBTables, DB, Grids, DBGrids, ADODB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
ADOConnection2: TADOConnection;
DataSource2: TDataSource;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TThreadQuery = class(TThread) { 声明线程类 }
private
FQuery: TADOQuery;
{ 线程中的查询组件 }
FDataSource: TDataSource;
{ 与查询组件相关的数据感知组件 }
procedure ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(Query: TADOQuery;
DataSource: TDataSource);
virtual;
{ 线程构造器 }
destructor Destroy;override;
end;
var
Form1: TForm1;
Q1, { 线程查询对象1 }
Q2: TThreadQuery;
{ 线程查询对象2 }
implementation
{$R *.dfm}
{ TThreadQuery类的实现 }
destructor TThreadQuery.Destroy;
begin
FQuery.Free;
FDataSource.Free;
inherited destroy;
end;
{ 连接数据查询组件和数据感知组件}
procedure TThreadQuery.ConnectDataSource;
begin
FDataSource.DataSet:= FQuery;{ 该方法在查询结束后才调用 }
end;
procedure TThreadQuery.Execute;{ 执行线程的方法 }
begin
try
FQuery.Open;
{ 打开查询 }
Synchronize(ConnectDataSource);{ 线程同步 }
except
ShowMessage('Query Error');
{ 线程异常 }
end;
end;
{ 线程查询类的构造器 }
constructor TThreadQuery.Create(Query: TADOQuery;
DataSource: TDataSource);
begin
FQuery := Query;
FDataSource := DataSource;
inherited Create(True);
FreeOnTerminate := False;
end;
{ 创建窗体时创建线程查询对象 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Q1 := TThreadQuery.Create(ADOQuery1, DataSource1);
Q2 := TThreadQuery.Create(ADOQuery2, DataSource2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Q1.Terminate;
{ 销毁之前终止线程执行 }
Q1.Destroy;
Q2.Terminate;
{ 销毁之前终止线程执行 }
Q2.Destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Q1.FQuery.Close;
Q2.FQuery.Close;
Q1 := TThreadQuery.Create(ADOQuery1, DataSource1);
Q2 := TThreadQuery.Create(ADOQuery2, DataSource2);
Q1.Resume;
Q2.Resume;
end;
end.
===============================
在重复执行-->procedure TForm1.Button1Click(Sender: TObject);
提示"Thread Error: 拒绝访问。
如果修改?
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DBTables, DB, Grids, DBGrids, ADODB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
ADOConnection2: TADOConnection;
DataSource2: TDataSource;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TThreadQuery = class(TThread) { 声明线程类 }
private
FQuery: TADOQuery;
{ 线程中的查询组件 }
FDataSource: TDataSource;
{ 与查询组件相关的数据感知组件 }
procedure ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(Query: TADOQuery;
DataSource: TDataSource);
virtual;
{ 线程构造器 }
destructor Destroy;override;
end;
var
Form1: TForm1;
Q1, { 线程查询对象1 }
Q2: TThreadQuery;
{ 线程查询对象2 }
implementation
{$R *.dfm}
{ TThreadQuery类的实现 }
destructor TThreadQuery.Destroy;
begin
FQuery.Free;
FDataSource.Free;
inherited destroy;
end;
{ 连接数据查询组件和数据感知组件}
procedure TThreadQuery.ConnectDataSource;
begin
FDataSource.DataSet:= FQuery;{ 该方法在查询结束后才调用 }
end;
procedure TThreadQuery.Execute;{ 执行线程的方法 }
begin
try
FQuery.Open;
{ 打开查询 }
Synchronize(ConnectDataSource);{ 线程同步 }
except
ShowMessage('Query Error');
{ 线程异常 }
end;
end;
{ 线程查询类的构造器 }
constructor TThreadQuery.Create(Query: TADOQuery;
DataSource: TDataSource);
begin
FQuery := Query;
FDataSource := DataSource;
inherited Create(True);
FreeOnTerminate := False;
end;
{ 创建窗体时创建线程查询对象 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Q1 := TThreadQuery.Create(ADOQuery1, DataSource1);
Q2 := TThreadQuery.Create(ADOQuery2, DataSource2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Q1.Terminate;
{ 销毁之前终止线程执行 }
Q1.Destroy;
Q2.Terminate;
{ 销毁之前终止线程执行 }
Q2.Destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Q1.FQuery.Close;
Q2.FQuery.Close;
Q1 := TThreadQuery.Create(ADOQuery1, DataSource1);
Q2 := TThreadQuery.Create(ADOQuery2, DataSource2);
Q1.Resume;
Q2.Resume;
end;
end.
===============================
在重复执行-->procedure TForm1.Button1Click(Sender: TObject);
提示"Thread Error: 拒绝访问。
如果修改?