S
szkok
Unregistered / Unconfirmed
GUEST, unregistred user!
大侠打救我!为什么在线程里打开TClientDataSet有问题?出错后再去打开的话就死机了,我想应是线程没释放,对这此意外事件,应在哪释放线程?第一次用线程,多多指教。
我的代码(我简化了的)如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBClient, MConnect, SConnect;
type
TForm1 = class(TForm)
SocketConnection1: TSocketConnection;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TMyThread.Execute;
begin
FreeOnTerminate:=True;
Form1.ClientDataSet1.Close;
Form1.ClientDataSet1.CommandText:='select * from Unit';
Form1.ClientDataSet1.Open;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mm:TMyThread;
begin
mm:=TMyThread.Create(False);
end;
end.
我的代码(我简化了的)如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBClient, MConnect, SConnect;
type
TForm1 = class(TForm)
SocketConnection1: TSocketConnection;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TMyThread.Execute;
begin
FreeOnTerminate:=True;
Form1.ClientDataSet1.Close;
Form1.ClientDataSet1.CommandText:='select * from Unit';
Form1.ClientDataSet1.Open;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mm:TMyThread;
begin
mm:=TMyThread.Create(False);
end;
end.