下面例子是在线程中,循环100次,每次5个子线程写内容到数据库中,使用adoquery循环添加数据,但内存不断增长,无法释放,帮忙看看,谢谢了!对了,也试过动态创建ADOConnection,每个AdoQuery 对应一个ADOConnection,但结果一样,内存不断增长,不能释放。d7+Access
以下为例子中的代码 :function Tuser_reg_thread.adduser(r_url:string):boolean;
var
ado_thread_user_reg:TADOQuery;
begin
try
ado_thread_user_reg:= TADOQuery.Create(nil);
ado_thread_user_reg.Connection:=frm_main.ADOConnection1;
try
with ado_thread_user_reg do
begin
close;
sql.Clear;
sql.Text:='select * from [url] where 1=2';
open;
append;
fieldbyname('url').AsString:=r_url;
post;
end;
finally
FreeAndNil(ado_thread_user_reg);
end;
result:=true;
except
result:=false;
end;
end;
procedure Tuser_reg_thread.Execute;
var
i: integer;
begin
try
for i:=1 to rnum do
begin
frm_main.ListBox1.Items.Add(rurl+' 第 '+inttostr(i)+' 个');
cs.Enter;
adduser(rurl);
cs.Leave;
end;
finally
sendmessage(frm_main.ListBox1.Handle,WM_VSCROLL,SB_BOTTOM,0);
end;
end;
//构造函数
constructor Tuser_reg_thread.create(url:string;num:integer);
begin
FreeOnTerminate := true;
rURL := url;
rnum:=num;
{构造完毕立刻执行}
inherited Create(false);
end;destructor Tuser_reg_thread.Destroy;
begin
WaitExecute;
inherited;
end;procedure Tuser_reg_thread.WaitExecute;
begin
Terminate;
WaitFor;
end; procedure Ttestthread.Execute;
var
r_url:string;
i: integer;
begin
try
frm_main.ListBox1.Items.Add('开始');
for i:=0 to 100 do
begin
r_url:='http'+inttostr(i);
u_reg:= Tuser_reg_thread.Create(r_url,5);
sleep(300);
end;
except
frm_main.ListBox1.Items.Add('出现未知错误');
end;
end;initialization
cs:=TCriticalSection.Create; //初始化
finalization
cs.Free; //删除
解决方案 »
- 怪事,MEMO载入TXT文件档不完整
- DBGRIDEH问题
- 一定要高手帮助!SDK应用中VC源码转Delphi,解决问题给高分并请客!先谢过了!!
- ★★★★★第三十六期:CSDN论坛秀-Delphi版-本期作秀:liangqingzhi(老之)
- 关注中国程序员,关注中国软件业的请进,欢迎评论
- 为什么我这个异常处理没有用啊?
- 有一个页面上有三个空白需要填写(“姓名”“年龄”“学号”),其中至少要填一项,只用一个“提交”BUTTON,从一个表上查询,请问SQL语
- 请问delphi6.0中的adotable的用法?
- 一个最有意思的贴字:)
- 字符串函数?
- 循环显示"无响应"问题
- Printer打印问题,可否实现横排和竖排混合的打印?
sql.Clear;
sql.Add('insert into url(url) values ('''+r_url+''')');
execsql;
还有
如果ado_thread_user_reg.Connection:=frm_main.ADOConnection1;这句出现异常
ado_thread_user_reg是释放不了的 应该finally 镶嵌except吧
另外,附带一句,你线程里使用了主线程(frm_main上的一些对象)里的一些对象,需要用Synchronize同步。
frm_main.ListBox1.Items.Add(rurl+' 第 '+inttostr(i)+' 个');
cs.Enter;//可能失败,直接跳到finally后面,会引起无法预知问题
adduser(rurl);
cs.Leave;