L
lwbcool
Unregistered / Unconfirmed
GUEST, unregistred user!
========
procedure TForm1.N5Click(Sender: TObject);
begin
Form2:=TForm2.Create(Self);
try
Form2.Show;
except
Form2.Free;
Form2.Close;
end;
end;
===============
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,activex,ADODB,DB;
type
TThreadSelect= class(TThread) { 声明线程类 }
private
QueryCondition: String;
{ 线程中的查询组件 }
Dqquery:TAdoquery;
procedure ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(suspended: Boolean;date:String);
overload;
{ 线程构造器 }
destructor Destroy;override;
end;
TForm2 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
ThreadSelect: TThreadSelect;
{ 线程查询对象1 }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
{ TThreadQuery类的实现 }
destructor TThreadSelect.Destroy;
begin
Dqquery.Free;
Dqquery.Connection.Close;
inherited destroy;
end;
{ 连接数据查询组件和数据感知组件}
procedure TThreadSelect.ConnectDataSource;
begin
form1.DataSource1.DataSet:= Dqquery;{ 该方法在查询结束后才调用 }
Form1.DBGrid1.DataSource:=form1.DataSource1;
end;
{ 执行线程的方法 }
procedure TThreadSelect.Execute;
var
zoneno,spotno,upspotno:String;
begin
coInitialize(nil);
try
Dqquery:=TAdoquery.Create(nil);
Dqquery.Connection:=Form1.ADOConnection1;
with Dqquerydo
begin
if not active then
begin
Dqquery.close;
Dqquery.sql.clear;
//Dqquery.sql.add('select zoneno ,spotno,upspotno from updatespot where upspotno is not null ');
Dqquery.sql.add('select zoneno ,spotno,upspotno from updatespot where upspotno is not null ');
Dqquery.sql.add(' and zoneno='''+QueryCondition+'''');
Form1.displaymsg(CMD,sql.text,'1');
try
Dqquery.open;
except
Form1.displaymsg(Err,'数据库出错','1');
Form1.displaymsg(FRTREP,'操作','1');
exit;
end;
Synchronize(ConnectDataSource);{ 线程同步 }
end;
end;
Form1.displaymsg(Meg,'执行完成!','1');
ShowMessage('执行完成!');
except
ShowMessage('线程异常!');
{ 线程异常 }
//Dqquery.Free;
end;
CoUninitialize;
//Dqquery.Connection.Close;
end;
{ 线程查询类的构造器 }
constructor TThreadSelect.Create(suspended: Boolean;date:String);
begin
QueryCondition:=date;
// FreeOnTerminate := false;
inherited Create(suspended);
end;
procedure TForm2.Button1Click(Sender: TObject);
var
condition :String;
begin
Form2.Hide ;
condition:=trim(Edit1.Text);
try
Form1.displaymsg(start,'创建线程...','1');
ThreadSelect:= TThreadSelect.Create(True,condition);
ThreadSelect.Resume;
Form1.displaymsg(start,'创建线程成功!','1');
except
Form1.displaymsg(Err,'创建线程失败!','1');
end;
//try
Form1.displaymsg(start,'创建线程完成!','1');
end;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
//application.Terminate;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
//ThreadSelect.Terminate;
{ 销毁之前终止线程执行 }
//ThreadSelect.Destroy;
application.Terminate;
end;
end.
==================
关闭窗口的时候,报“无效的句柄错误”
不知到时什么问题,规求!
procedure TForm1.N5Click(Sender: TObject);
begin
Form2:=TForm2.Create(Self);
try
Form2.Show;
except
Form2.Free;
Form2.Close;
end;
end;
===============
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,activex,ADODB,DB;
type
TThreadSelect= class(TThread) { 声明线程类 }
private
QueryCondition: String;
{ 线程中的查询组件 }
Dqquery:TAdoquery;
procedure ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(suspended: Boolean;date:String);
overload;
{ 线程构造器 }
destructor Destroy;override;
end;
TForm2 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
ThreadSelect: TThreadSelect;
{ 线程查询对象1 }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
{ TThreadQuery类的实现 }
destructor TThreadSelect.Destroy;
begin
Dqquery.Free;
Dqquery.Connection.Close;
inherited destroy;
end;
{ 连接数据查询组件和数据感知组件}
procedure TThreadSelect.ConnectDataSource;
begin
form1.DataSource1.DataSet:= Dqquery;{ 该方法在查询结束后才调用 }
Form1.DBGrid1.DataSource:=form1.DataSource1;
end;
{ 执行线程的方法 }
procedure TThreadSelect.Execute;
var
zoneno,spotno,upspotno:String;
begin
coInitialize(nil);
try
Dqquery:=TAdoquery.Create(nil);
Dqquery.Connection:=Form1.ADOConnection1;
with Dqquerydo
begin
if not active then
begin
Dqquery.close;
Dqquery.sql.clear;
//Dqquery.sql.add('select zoneno ,spotno,upspotno from updatespot where upspotno is not null ');
Dqquery.sql.add('select zoneno ,spotno,upspotno from updatespot where upspotno is not null ');
Dqquery.sql.add(' and zoneno='''+QueryCondition+'''');
Form1.displaymsg(CMD,sql.text,'1');
try
Dqquery.open;
except
Form1.displaymsg(Err,'数据库出错','1');
Form1.displaymsg(FRTREP,'操作','1');
exit;
end;
Synchronize(ConnectDataSource);{ 线程同步 }
end;
end;
Form1.displaymsg(Meg,'执行完成!','1');
ShowMessage('执行完成!');
except
ShowMessage('线程异常!');
{ 线程异常 }
//Dqquery.Free;
end;
CoUninitialize;
//Dqquery.Connection.Close;
end;
{ 线程查询类的构造器 }
constructor TThreadSelect.Create(suspended: Boolean;date:String);
begin
QueryCondition:=date;
// FreeOnTerminate := false;
inherited Create(suspended);
end;
procedure TForm2.Button1Click(Sender: TObject);
var
condition :String;
begin
Form2.Hide ;
condition:=trim(Edit1.Text);
try
Form1.displaymsg(start,'创建线程...','1');
ThreadSelect:= TThreadSelect.Create(True,condition);
ThreadSelect.Resume;
Form1.displaymsg(start,'创建线程成功!','1');
except
Form1.displaymsg(Err,'创建线程失败!','1');
end;
//try
Form1.displaymsg(start,'创建线程完成!','1');
end;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
//application.Terminate;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
//ThreadSelect.Terminate;
{ 销毁之前终止线程执行 }
//ThreadSelect.Destroy;
application.Terminate;
end;
end.
==================
关闭窗口的时候,报“无效的句柄错误”
不知到时什么问题,规求!