仿某位大侠的线程程序,不知道问题在哪,是数据库的。 ( 积分: 50 )

  • 主题发起人 主题发起人 blkfires
  • 开始时间 开始时间
B

blkfires

Unregistered / Unconfirmed
GUEST, unregistred user!
nit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,forms,
StdCtrls, DB, Grids, DBGrids, ADODB;

type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
dbthread = class(TThread)
private

protected
mquery:Tadoquery;
mconnection:Tadoconnection;
mdatasource:Tdatasource;
procedure Execute;
override;
procedure createconnection;
procedure hook;
public
constructor create(q:Tadoquery;conn:Tadoconnection;data:Tdatasource);virtual;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
constructor dbthread.create(q:Tadoquery;conn:Tadoconnection;data:Tdatasource);
begin

inherited create(true);
mquery:=q;
mconnection:=conn;
mdatasource:=data;
freeonterminate:=true;
resume;
end;
procedure dbthread.Execute;
begin

createconnection;
mquery.Open;
synchronize(hook);
end;
procedure dbthread.createconnection;
var
str:string;
begin
str:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fang;Data Source=FANG-A436922EE0';
mconnection.ConnectionString:=str;
mquery.Connection:=mconnection;
mquery.Sql.Clear;
mquery.SQL.Add('select * from t1');
end;
procedure dbthread.hook;
begin
mdatasource.DataSet:=mquery;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
dbthread.create(adoquery1,adoconnection1,datasource1);
end;

end.
可以运行,却没反映,不知道问题在哪,我是新手,大家帮忙。
另外象TADOQUERY这些组件是不是只能在有界面的单元里编辑啊,好象在新开的线程单元里不能编辑。
 
nit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,forms,
StdCtrls, DB, Grids, DBGrids, ADODB;

type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
dbthread = class(TThread)
private

protected
mquery:Tadoquery;
mconnection:Tadoconnection;
mdatasource:Tdatasource;
procedure Execute;
override;
procedure createconnection;
procedure hook;
public
constructor create(q:Tadoquery;conn:Tadoconnection;data:Tdatasource);virtual;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
constructor dbthread.create(q:Tadoquery;conn:Tadoconnection;data:Tdatasource);
begin

inherited create(true);
mquery:=q;
mconnection:=conn;
mdatasource:=data;
freeonterminate:=true;
resume;
end;
procedure dbthread.Execute;
begin

createconnection;
mquery.Open;
synchronize(hook);
end;
procedure dbthread.createconnection;
var
str:string;
begin
str:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fang;Data Source=FANG-A436922EE0';
mconnection.ConnectionString:=str;
mquery.Connection:=mconnection;
mquery.Sql.Clear;
mquery.SQL.Add('select * from t1');
end;
procedure dbthread.hook;
begin
mdatasource.DataSet:=mquery;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
dbthread.create(adoquery1,adoconnection1,datasource1);
end;

end.
可以运行,却没反映,不知道问题在哪,我是新手,大家帮忙。
另外象TADOQUERY这些组件是不是只能在有界面的单元里编辑啊,好象在新开的线程单元里不能编辑。
 
可以编辑处理的,可以设置属性这些的。
 
那当然没有反映了,你在TForm1.Button1Click的事件中只调用了dbthread.create(adoquery1,adoconnection1,datasource1);而这个过程也没有调用dbthread.Execute;
dbthread.createconnection和dbthread.hook;你把这三个事件在TForm1.Button1Click的事件中只调用就可以,
 
或许在执行的过程中有错误, 你加一个TRY EXCEPT来捕捉一下吧.
还有一种可能就是你的T1表资料很多,还在送资料,也就是还没有执行到HOOK那一步.
 
把resume;
拿到外面来试试~~
 
procedure TForm1.Button1Click(Sender: TObject);
begin
dbthread.create(adoquery1,adoconnection1,datasource1);
dbthread.Execute;//这行很重要,不要丢哦
end;
 
dbthread.Execute;当线程启动的时候自动调用的
inherited create(true);这句是指创建线程后挂起
另外线程类命名为Tdbthead较好
因该这样用吧
procedure TForm1.Button1Click(Sender: TObject);
var
dbthread: Tdbthead
begin
dbthead := Tdbthread.create(adoquery1,adoconnection1,datasource1);
dbthead.resume;
end;
 
后退
顶部