entironment 2007年03月23日
关于ado多线程的问题,请大家帮忙看看
unit Main;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
ADOQuery1: TADOQuery;
Edit1: TEdit;
Lab: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TTestThread = class(TThread)
private
a,count:integer;
betweentime :TDateTime;
procedure GiveCountnum;
protected
procedure Execute; override;
public
constructor Create(b:integer); virtual;
end;


var
Form1: TForm1;
myado:array of TADOQuery;
testThread : array of TTestThread ;
myLab:array of TLabel;
between:Tdatetime;
countnum:integer;
implementation

{$R *.dfm}
constructor TTestThread.Create(b: integer);
begin
a:=b;
inherited Create(false);
FreeOnTerminate := False;
end;

procedure TTestThread.GiveCountnum;
begin
countnum:=count;
between:=betweentime;
end;


procedure TTestThread.Execute;
var
begintime,endtime:TDateTime;
begin
//coinitialize(nil);
begintime:=time;
myado[a].Open; //执行到这里为什么开始循环了,不继续向下执行。
count:=myado[a].Fields[0].AsInteger;
endtime:=time;
betweentime:=endtime-begintime;
Synchronize(GiveCountnum);
//couninitialize;
//FreeOnTerminate := false;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i,j:integer;
begin
setlength(myado,strtoint(edit1.Text));
setlength(testThread,strtoint(edit1.Text));
setlength(mylab,strtoint(edit1.Text));
for i:=0 to strtoint(edit1.Text)-1 do
begin
myado[i]:=TADOQuery.Create(self);
myado[i].ConnectionString:='Provider=SQLOLEDB.1;Password=jycc;Persist Security Info=True;User ID=sa;Initial Catalog=gome-new';
myado[i].Close;
myado[i].SQL.Clear;
myado[i].SQL.Add('select count(*) from usr_zxthh');

myLab[i]:=TLabel.Create(self);
myLab[i].Parent:=self;
myLab[i].Left:=i*200;
myLab[i].Top:=50;

end;
for j:=0 to strtoint(edit1.Text)-1 do
begin
testThread[j] := TTestThread.Create(j);
//if testThread[i].Suspended then testThread[i].Resume else testThread[i].Suspend;
myLab[j].Caption:='线程'+inttostr(j+1)+'执行时间:'+TimetoStr(between)+'结果:'+inttostr(countnum);
end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
var
k:integer;
begin
for k:=0 to strtoint(edit1.Text)-1 do
begin
testThread[k].Terminate;
testThread[k].Destroy;
end;
end;

end.

为什么执行到线程的execute方法中,开始循环了。
...全文
267 点赞 收藏 8
写回复
8 条回复

还没有回复,快来抢沙发~

发动态
发帖子
数据库相关
创建于2007-08-02

1209

社区成员

8.8w+

社区内容

Delphi 数据库相关
社区公告
暂无公告