S
sstsgqyj
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Database1: TDatabase;
Database2: TDatabase;
Query1: TQuery;
Query2: TQuery;
DataSource1: TDataSource;
DataSource2: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
Button1: TButton;
Session1: TSession;
Session2: TSession;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TThreadQuery = class(TThread) //声明线程类
private
FQuery: TQuery;
//线程中的查询事件
FDataSource: TDataSource;
//与查询组件相关的数据感知组件
procedure ConnectDataSource;
//连接数据查询组件与感知组件的方法
protected
procedure Execute;
override;
//执行线程的方法
public
constructor Create(Query: TQuery;
DataSource: TDataSource);
virtual;
//线程构造器
end;
var
Form1: TForm1;
Q1, Q2: TThreadQuery;
//线程查询对象
implementation
{$R *.dfm}
procedure TThreadQuery.ConnectDataSource;
//该方法在查询结束后才调用
begin
FDataSource.DataSet := FQuery;
end;
procedure TThreadQuery.Execute;
//执行线程的方法
begin
try
FQuery.Open;
//打开查询
Synchronize(ConnectDataSource);
//线程同步
except
showmessage('线程异常');
end;
end;
constructor TThreadQuery.Create(Query: TQuery;
DataSource: TDataSource);
//线程查询类的构造器
begin
FQuery := Query;
FDataSource := DataSource;
inherited Create(True);
FreeOnTerminate := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
//创建窗体时创建线程查询对象
begin
Q1 := TThreadQuery.Create(Query1, DataSource1);
Q2 := TThreadQuery.Create(Query2, DataSource2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
//销毁窗体时销毁线程查询对象
begin
Q1.Terminate;
//销毁之前终止线程执行
Q1.Destroy;
Q2.Terminate;
//销毁之前终止线程执行
Q2.Destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
//开始线程、继续执行线程、挂起线程
begin
if Q1.Suspended then
Q1.Resume
else
Q1.Suspend;
if Q2.Suspended then
Q2.Resume
else
Q2.Suspend;
end;
end.
===============================
在重复执行-->procedure TForm1.Button1Click(Sender: TObject);
提示"Thread Error: 拒绝访问。 (5)",请问为何会这样的啊?
只剩余那么少分了,多谢各位帮帮忙
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Database1: TDatabase;
Database2: TDatabase;
Query1: TQuery;
Query2: TQuery;
DataSource1: TDataSource;
DataSource2: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
Button1: TButton;
Session1: TSession;
Session2: TSession;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TThreadQuery = class(TThread) //声明线程类
private
FQuery: TQuery;
//线程中的查询事件
FDataSource: TDataSource;
//与查询组件相关的数据感知组件
procedure ConnectDataSource;
//连接数据查询组件与感知组件的方法
protected
procedure Execute;
override;
//执行线程的方法
public
constructor Create(Query: TQuery;
DataSource: TDataSource);
virtual;
//线程构造器
end;
var
Form1: TForm1;
Q1, Q2: TThreadQuery;
//线程查询对象
implementation
{$R *.dfm}
procedure TThreadQuery.ConnectDataSource;
//该方法在查询结束后才调用
begin
FDataSource.DataSet := FQuery;
end;
procedure TThreadQuery.Execute;
//执行线程的方法
begin
try
FQuery.Open;
//打开查询
Synchronize(ConnectDataSource);
//线程同步
except
showmessage('线程异常');
end;
end;
constructor TThreadQuery.Create(Query: TQuery;
DataSource: TDataSource);
//线程查询类的构造器
begin
FQuery := Query;
FDataSource := DataSource;
inherited Create(True);
FreeOnTerminate := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
//创建窗体时创建线程查询对象
begin
Q1 := TThreadQuery.Create(Query1, DataSource1);
Q2 := TThreadQuery.Create(Query2, DataSource2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
//销毁窗体时销毁线程查询对象
begin
Q1.Terminate;
//销毁之前终止线程执行
Q1.Destroy;
Q2.Terminate;
//销毁之前终止线程执行
Q2.Destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
//开始线程、继续执行线程、挂起线程
begin
if Q1.Suspended then
Q1.Resume
else
Q1.Suspend;
if Q2.Suspended then
Q2.Resume
else
Q2.Suspend;
end;
end.
===============================
在重复执行-->procedure TForm1.Button1Click(Sender: TObject);
提示"Thread Error: 拒绝访问。 (5)",请问为何会这样的啊?
只剩余那么少分了,多谢各位帮帮忙