X
xiaqiapeng
Unregistered / Unconfirmed
GUEST, unregistred user!
程序中一窗体使用线程查询数据库,
运行到一半,我终止了该线程,但是关闭窗体时出现错误:在异步运行时,操作不能被执行。
有人知道是什么原因吗?
代码如下:
1.启动线程
procedure TForm1.Button1Click(Sender: TObject);
begin
myThread1:=TThread1.Create(false);
while (myThread1.isfinish=false)do
begin
progressbar1.Position:= progressbar1.Position+1;
if progressbar1.Position=100 then
progressbar1.Position:=0;
Application.ProcessMessages;
end ;
end;
2.线程处理时的代码:
procedure TThread1.Execute;
begin
Coinitialize(nil);
Process;
CoUninitialize();
end;
procedure TThread1.process;
begin
isfinish:=false;
form1.ADOQuery1.Close;
form1.ADOQuery1.SQL.Clear;
sql:='代码太长,省略';
form1.ADOQuery1.SQL.Add(sql);
form1.ADOQuery1.Open;
form1.edit1.text:='查询到'+inttostr(form1.ADOQuery1.RecordCount)+'条记录!';
isfinish:=true;
end;
3.终止线程的代码
procedure TForm1.Button12Click(Sender: TObject);
begin
if assigned(myThread1) then
begin
ADOQuery1.Close;
myThread1.suspend;
myThread1.isfinish:=true;
myThread1.terminate;
progressbar1.Position:=0;
end;
end;
4.窗体退出时的代码
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ADOConnection1.Close;
application.Terminate;
end;
运行到一半,我终止了该线程,但是关闭窗体时出现错误:在异步运行时,操作不能被执行。
有人知道是什么原因吗?
代码如下:
1.启动线程
procedure TForm1.Button1Click(Sender: TObject);
begin
myThread1:=TThread1.Create(false);
while (myThread1.isfinish=false)do
begin
progressbar1.Position:= progressbar1.Position+1;
if progressbar1.Position=100 then
progressbar1.Position:=0;
Application.ProcessMessages;
end ;
end;
2.线程处理时的代码:
procedure TThread1.Execute;
begin
Coinitialize(nil);
Process;
CoUninitialize();
end;
procedure TThread1.process;
begin
isfinish:=false;
form1.ADOQuery1.Close;
form1.ADOQuery1.SQL.Clear;
sql:='代码太长,省略';
form1.ADOQuery1.SQL.Add(sql);
form1.ADOQuery1.Open;
form1.edit1.text:='查询到'+inttostr(form1.ADOQuery1.RecordCount)+'条记录!';
isfinish:=true;
end;
3.终止线程的代码
procedure TForm1.Button12Click(Sender: TObject);
begin
if assigned(myThread1) then
begin
ADOQuery1.Close;
myThread1.suspend;
myThread1.isfinish:=true;
myThread1.terminate;
progressbar1.Position:=0;
end;
end;
4.窗体退出时的代码
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ADOConnection1.Close;
application.Terminate;
end;