简单的线程代码出错,帮忙看看原因.谢谢. (100分)

  • 主题发起人 主题发起人 死灰
  • 开始时间 开始时间

死灰

Unregistered / Unconfirmed
GUEST, unregistred user!
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;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

TTThread = class(TThread)
private
TADOconn: TADOConnection;
protected
procedure Execute;virtual;
public
constructor Create(ADOConnection: TADOConnection);
end;

var
Form1: TForm1;
Hread: TTThread;

implementation
{$R *.dfm}
{ TThread类的实现 }
constructor TTThread.Create(ADOConnection: TADOConnection);
begin

TADOconn := ADOConnection;
inherited Create(True);
FreeOnTerminate := false;
end;

procedure TTThread.Execute ;{ 执行线程的方法 }
begin

showmessage('开始连接!');
try
TADOconn.Connected := true;
{ 连接 }
showmessage('Connected!')
except
ShowMessage('Connected Error');
{ 连接异常 }
end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

Hread := TTThread.Create(ADOConnection1);
hread.Resume ;
{这里出错}
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

if hread.Suspended then

begin

Hread.Terminate;
{ 销毁之前终止线程执行 }
Hread.Destroy;
end
else

showmessage('没有线程');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin

hread.Terminate;
{ 销毁之前终止线程执行 }
hread.Destroy;
end;

end.

当按下Button1时候运行出错报告:
procedure TForm1.Button1Click(Sender: TObject)
project project1.exe raised exception class EAbstract.Error with message 'Abstract.Error'.Process stopped . use setup or Run to continue.
刚学DELPHI.不要见笑.
线程不能运行.该怎样修改??
 
procedure Execute;
override;
另外ShowMessage()最好放在Synchronize中
 
根据你的提示调好了.
procedure TTThread.Execute ;{ 执行线程的方法 } 过程可以运行.
但TADOconn.Connected := true;出错.错误提示:
Project.Project1.exe raised exception class EQleException with message '尚未调用
CoInitialize.'.Process stopped.Use Setup or RUn to continue.
CoInitialize需要怎么调用?或者是不是代码要放在Synchronize中调用?
还有我用win2000+Delphi6+oracle8.17
 
注意:你的是TADOconn是在主线程中创建的,然后你在另一个线程中使用它,
这样做要求TADOconn支持线程才行(也就是要多写一些线程同步的代码)。
如果TADOconn不支持多线程,则会导致不可预料的后果。
CoInitializa调用如下(要USE ActiveX):
unit Unit1;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Buttons, ExtCtrls, DB, ADODB, ActiveX;

type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

TTThread = class(TThread)
private
TADOconn: TADOConnection;
protected
procedure Execute;
override;////
public
constructor Create(ADOConnection: TADOConnection);
end;

var
Form1: TForm1;
Hread: TTThread;

implementation
{$R *.dfm}
{ TThread类的实现 }
constructor TTThread.Create(ADOConnection: TADOConnection);
begin

TADOconn := ADOConnection;
inherited Create(True);
FreeOnTerminate := false;
end;

procedure TTThread.Execute ;{ 执行线程的方法 }
begin

CoInitializa(nil);////
showmessage('开始连接!');
try
TADOconn.Connected := true;
{ 连接 }
showmessage('Connected!')
except
ShowMessage('Connected Error');
{ 连接异常 }
end;

CoUnInitializa;////
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

Hread := TTThread.Create(ADOConnection1);
hread.Resume ;
{这里出错}
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

if hread.Suspended then

begin

Hread.Terminate;
{ 销毁之前终止线程执行 }
Hread.Destroy;
end
else

showmessage('没有线程');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin

hread.Terminate;
{ 销毁之前终止线程执行 }
hread.Destroy;
end;

end.

 
接受答案了.
 

Similar threads

S
回复
0
查看
751
SUNSTONE的Delphi笔记
S
I
回复
0
查看
740
import
I
S
回复
0
查看
830
SUNSTONE的Delphi笔记
S
后退
顶部