关于ado多线程的问题,请大家帮忙看看

entironment 2007-03-23 12:54:16
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方法中,开始循环了。
...全文
332 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
net3 2007-03-27
  • 打赏
  • 举报
回复
net321@hotmail.com
交流。
chenzhuo 2007-03-27
  • 打赏
  • 举报
回复
我靠,马甲!
entironment 2007-03-25
  • 打赏
  • 举报
回复
在顶,周末那位高手抽出点时间给看看啊
chenzhuo 2007-03-23
  • 打赏
  • 举报
回复
ADO没有线程安全,把Execute里的代码放在一个过程里,用
Synchronize()去执行!
entironment 2007-03-23
  • 打赏
  • 举报
回复
没人帮帮忙吗?自己顶!谢谢各位了
entironment 2007-03-23
  • 打赏
  • 举报
回复
楼上的大哥,说的是,小弟初学多线程,还请指点如何同步。这个程序该怎么改。
universe 2007-03-23
  • 打赏
  • 举报
回复
当然要开始循环了,如果和单线程一样一步一步走,还要多线程干什么

做多线程不是一件容易的事情,要充分考虑到线程中的同步问题

否则很容易出现多线程同时访问一个。。。的问题,出现异常
entironment 2007-03-23
  • 打赏
  • 举报
回复
procedure TTestThread.GiveCountnum;
begin
begintime:=time;
myado[a].Open;
count:=myado[a].Fields[0].AsInteger;
endtime:=time;
betweentime:=endtime-begintime;
countnum:=count;
between:=betweentime;
end;

procedure TTestThread.Execute;
begin
Synchronize(GiveCountnum);
end;

to:chenzhuo(灼) 是这样的吗,还是不行啊,谢谢!

2,507

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧