Thread Error: 拒绝访问。 (5)(50分)

  • 主题发起人 主题发起人 lwbcool
  • 开始时间 开始时间
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: 拒绝访问。
如果修改?
 
都执行什么代码了? 问问题也要表达完整.
 
连接数据库的ADOQuery1,没有释放
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Tabs, ExtCtrls, ComCtrls, IdHTTP, Unit2, DB, ADODB,
Grids, DBGrids;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
ProgressBar1: TProgressBar;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
//弹出信息框
procedure ThreadExit(sender: TObject);
public
{ Public declarations }
end;

var
Form1: TForm1;
Thread1: array of T1;
// 定义线程数组
n: integer = 0;
bool: boolean = True;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
Sum:integer;
begin
if bool then
begin
n :=0;
Sum :=strtoint(edit3.text);//Memo1.lines.count;
SetLength(Thread1,Sum);
// 动态设置线程的数量
ProgressBar1.Min :=0;
ProgressBar1.Max :=sum;
ProgressBar1.Step :=1;
ProgressBar1.Position :=0;
for i := 1 to Sum do
begin
Thread1 := T1.Create(ADOQuery1,i);
Thread1.OnTerminate := ThreadExit;
// ADOQuery1.Free;
end;
end;
bool := False;
end;

procedure TForm1.ThreadExit(sender: TObject);
begin
ProgressBar1.StepIt;
inc(n);
if N = strtoint(edit3.Text) then
begin
bool := true;
exit;
end;
end;

end.

unit Unit2;
interface
uses
Classes,StdCtrls,Windows,SysUtils,wininet,IdHTTP,ADODB;
var
CS:TRTLCriticalSection;
//定义全局临界区
type
T1 = class(TThread)
private
TmpM1,TmpM2,TmpM3: TMemo;
ADO: TADOQuery;
TmpNum: integer;
Str :string;
procedure DataMemo;
protected
procedure Execute;
override;
public
constructor Create(M1:TADOQuery;
Num: integer);
end;

implementation
uses Unit1;
{ T1 }
constructor T1.Create(M1:TADOQuery;
Num: integer);
begin
TmpNum := Num;
// 传递参数
ado :=M1;
// 绑定控件
FreeOnTerminate := True;
// 自动删除
InitializeCriticalSection(CS);
//初始化临界区
inherited Create(False);
// 直接运行
end;

procedure T1.DataMemo;
begin
Ado.Edit;
ado.Insert;
ado.FieldByName('TT').AsString:=inttostr(TmpNum);
ado.Post;
end;

procedure T1.Execute;
begin
EnterCriticalSection(cs);
Synchronize(DataMemo);
// 同步
LeaveCriticalSection(CS);
//退出临界区
end;

end.

问题是我可编译,但是运行是发生错误,请高手帮我
 
后退
顶部