关闭窗口的时候,报“无效的句柄错误”(50分)

  • 主题发起人 主题发起人 lwbcool
  • 开始时间 开始时间
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.

==================
关闭窗口的时候,报“无效的句柄错误”
不知到时什么问题,规求!
 
在线程.Execute中的循环加一个标志判断,当标志为假时,线程跳出循环.在关闭窗体时把标志置为假.
给分吧
 
Free之后还能Close?
 
procedure TForm1.N5Click(Sender: TObject);
begin
Form2:=TForm2.Create(Self);
try
Form2.Show;
except
Form2.Free;
Form2.Close;
end;
end;

你先free,Form2就不存在了,无法close
 
[:D]try
Form2:=TForm2.Create(nil);
finally
freeandnil(form2);
end;
既然是self那干什么要主动去free,当然你都free了还可以close啊
close就是关掉,把窗体hide,
而且你是except出现exception的时候,你可以单步一下,[:D]
F7步入,F8步出,不懂的问一下delphi,它是一个非常好的老师
 
用freeandnil(form2)
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
783
import
I
后退
顶部