死
死灰
Unregistered / Unconfirmed
GUEST, unregistred user!
数据库:Oracle 8.17
当连接数据库的时候有一段时间是需要等待的,并且哪段时间内Form不能操作..
也就不能取消连接.只有等连接结束后才能操作.
我需要运行的结果是可以点击连接,然后可以操作取消连接..
下面是我的代码..运行结果跟不用线程是一样.也是在连接时候是停顿的..
是不是代码写错了.
有没有写好的程序发我看看?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Buttons, ExtCtrls, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo1: TMemo;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTThread = class(TThread)
private
TADOconn: TADOConnection;
procedure ConnectDB;
protected
procedure Execute;override;
public
constructor Create(ADOConnection: TADOConnection);
published
destructor Destroy;
override;
end;
var
Form1: TForm1;
Hread: TTThread;
implementation
{$R *.dfm}
{ TThread类的实现 }
constructor TTThread.Create(ADOConnection: TADOConnection);
begin
inherited Create(true);
FreeOnTerminate := true;
TADOconn := ADOConnection;
Suspended := false;
end;
procedure TTThread.Execute ;{ 执行线程的方法 }
begin
Synchronize(ConnectDB);
// Setup the ProgressBar
end;
procedure TTThread.ConnectDB;
// Update ProgressBar
begin
try
form1.Memo1.Text := 'Connected....';//不会显示..必须通过form1.refresh才会显示.
TADOconn.Connected := true;
// 连接
form1.Memo1.Text := 'Connected';
except
form1.Memo1.Text := 'Connect Error';
end;
end;
destructor TTThread.Destroy;
begin
inherited destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Hread := TTThread.Create(ADOConnection1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
hread.Suspend ;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
hread.Resume ;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Hread.Terminate ;
{ 销毁之前终止线程执行 }
Hread := nil;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if Hread = nil then
showmessage('nil');
end;
end.
当连接数据库的时候有一段时间是需要等待的,并且哪段时间内Form不能操作..
也就不能取消连接.只有等连接结束后才能操作.
我需要运行的结果是可以点击连接,然后可以操作取消连接..
下面是我的代码..运行结果跟不用线程是一样.也是在连接时候是停顿的..
是不是代码写错了.
有没有写好的程序发我看看?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Buttons, ExtCtrls, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo1: TMemo;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTThread = class(TThread)
private
TADOconn: TADOConnection;
procedure ConnectDB;
protected
procedure Execute;override;
public
constructor Create(ADOConnection: TADOConnection);
published
destructor Destroy;
override;
end;
var
Form1: TForm1;
Hread: TTThread;
implementation
{$R *.dfm}
{ TThread类的实现 }
constructor TTThread.Create(ADOConnection: TADOConnection);
begin
inherited Create(true);
FreeOnTerminate := true;
TADOconn := ADOConnection;
Suspended := false;
end;
procedure TTThread.Execute ;{ 执行线程的方法 }
begin
Synchronize(ConnectDB);
// Setup the ProgressBar
end;
procedure TTThread.ConnectDB;
// Update ProgressBar
begin
try
form1.Memo1.Text := 'Connected....';//不会显示..必须通过form1.refresh才会显示.
TADOconn.Connected := true;
// 连接
form1.Memo1.Text := 'Connected';
except
form1.Memo1.Text := 'Connect Error';
end;
end;
destructor TTThread.Destroy;
begin
inherited destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Hread := TTThread.Create(ADOConnection1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
hread.Suspend ;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
hread.Resume ;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Hread.Terminate ;
{ 销毁之前终止线程执行 }
Hread := nil;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if Hread = nil then
showmessage('nil');
end;
end.