在子线程中查询数据库为什么主界面会卡住呢

yezhizi 2017-07-13 06:15:28
constructor TTestThread.create(CreateSuspended: Boolean);
begin
inherited create(CreateSuspended);
end;

destructor TTestThread.Destroy;
begin

inherited;
end;

procedure TTestThread.execute;
var
con1: TADOConnection;
ADOCommand1: TADOCommand;
begin
inherited;
CoInitialize(nil);
con1 := TADOConnection.Create(nil);
con1.ConnectionString := 'Provider=SQLOLEDB.1;Password=11;Persist Security Info=True;User ID=sa;Initial Catalog=aa;Data Source=127.0.0.1';
con1.Connected := True;
ADOCommand1 := TADOCommand.Create(nil);
ADOCommand1.Connection := con1;
ADOCommand1.CommandText := 'SELECT * FROM TESTTBL';
ADOCommand1.Execute;
ADOCommand1.Free;
con1.Close;
CoUninitialize;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
thread:TTestThread;
begin
thread := TTestThread.create(True);
thread.FreeOnTerminate := True;
thread.execute;
//执行这个后如果查询过程比较长的话整个界面被卡住了
end;



1、创建子线程
2、在子线程中执行查询,但是如果查询过程比较长的话整个界面被卡住了,界面都无法拖拽,为什么呀

如代码所示
...全文
1351 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyhoo163 2018-07-01
  • 打赏
  • 举报
回复
线程占用时间加大,造成主线程迟缓。
xiaocongzhi 2018-07-01
  • 打赏
  • 举报
回复
唤醒线程Thread.Resume(新版本好像是Start),别在主线程里调用线程执行函数execute
BlueStorm 2018-06-27
  • 打赏
  • 举报
回复
如果是delphi XE7及以上的版本,可以用System.Threading的TTask.Run来运行线程, 这样写起来简单多了:
uses System.Threading;
.............................................
procedure TForm1.Button1Click(Sender: TObject);
begin
TTask.Run(procedure
var
con1: TADOConnection;
ADOCommand1: TADOCommand;
begin
inherited;
CoInitialize(nil);
con1 := TADOConnection.Create(nil);
con1.ConnectionString := 'Provider=SQLOLEDB.1;Password=11;Persist Security Info=True;User ID=sa;Initial Catalog=aa;Data Source=127.0.0.1';
con1.Connected := True;
ADOCommand1 := TADOCommand.Create(nil);
ADOCommand1.Connection := con1;
ADOCommand1.CommandText := 'SELECT * FROM TESTTBL';
ADOCommand1.Execute;
ADOCommand1.Free;
con1.Close;
CoUninitialize;
end);
end;
jjpweb 2017-09-22
  • 打赏
  • 举报
回复
引用 1 楼 linuxpingwangping 的回复:
你这哪里是在线程中运行查询, 是在主线程运行查询 覆盖TThread的execute方法。 主线程中不用调用thread.execute;, 调用thread.Resume
lyhoo163 2017-09-22
  • 打赏
  • 举报
回复
你的线程使用有问题,通常使用Delphi线程,三步: 一、声明 type TGaugeThread = class(Tthread) protected procedure Execute;override; end; 二、实现 procedure TGaugeThread.Execute; var i: integer; begin ThreadOn:=True; ThreadEnd:=False; FreeOnTerminate:=true; ...... ThreadOn:=False; end; 三、调用 if Not ThreadON then begin ..... MyThread:=TGaugeThread.Create(False); end;
BlueStorm 2017-09-21
  • 打赏
  • 举报
回复
另外,ado已经是很古老的东西,性能不好,可以改用UniDac或FireDac之类的。
BlueStorm 2017-09-21
  • 打赏
  • 举报
回复
thread.execute 改为 thread.start
doloopcn 2017-07-20
  • 打赏
  • 举报
回复
在执行线程前加一句:application.ProcessMessages
hongss 2017-07-19
  • 打赏
  • 举报
回复
引用 1 楼 linuxpingwangping 的回复:
你这哪里是在线程中运行查询, 是在主线程运行查询 覆盖TThread的execute方法。 主线程中不用调用thread.execute;, 调用thread.Resume
pathletboy 2017-07-18
  • 打赏
  • 举报
回复
你线程的使用方法不正确。
  • 打赏
  • 举报
回复
你这哪里是在线程中运行查询, 是在主线程运行查询 覆盖TThread的execute方法。 主线程中不用调用thread.execute;, 调用thread.Resume

2,498

社区成员

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

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