Z
zxa
Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序中,使用线程完成数据库查询,而在主程序的INDY UDP 通讯中的UDPServerUDPRead事件中又向数据库中的表写入数据,如果udp未收到数据,线程查询很正常,主窗口也可以同时完成其他工作,可是在线程正在进行查询时一旦udp收到数据向数据库写入时,整个窗体死掉,直到线程完成查询为止,感觉就像查询线程变成了主线程.为什么?!
线程程序如下:
..
tquerythread= class(tthread)
private
FADOConnection:TADOConnection;
FADOQuery:TADOQuery;
fdatasource:tdatasource;
fqueryexception:exception;
procedure connectdatasource;
procedure showqryerror;
protected
procedure execute;override;
public
constructor create(adoconnection:tadoconnection;adoquery:tadoquery;datasource:tdatasource);virtual;
end;
...
...
constructor tquerythread.create(adoconnection:tadoconnection;adoquery:tadoquery;datasource:tdatasource);
begin
inherited create(true);
fadoconnection:=adoconnection;
fadoquery:=adoquery;
fdatasource:=datasource;
freeonterminate:=true;
resume;
end;
procedure tquerythread.execute;
begin
try
fadoquery.Open;
application.ProcessMessages;
//connectdatasource;
synchronize(connectdatasource);
except
end;
end;
procedure tquerythread.connectdatasource;
begin
fdatasource.dataset:=fadoquery;
end;
procedure tquerythread.showqryerror;
begin
application.ShowException(fqueryexception);
end;
procedure runbackgroundquery(adoconnection:tadoconnection;adoquery:tadoquery;datasource:tdatasource);
begin
tquerythread.create(adoconnection,adoquery,datasource);
end;
线程程序如下:
..
tquerythread= class(tthread)
private
FADOConnection:TADOConnection;
FADOQuery:TADOQuery;
fdatasource:tdatasource;
fqueryexception:exception;
procedure connectdatasource;
procedure showqryerror;
protected
procedure execute;override;
public
constructor create(adoconnection:tadoconnection;adoquery:tadoquery;datasource:tdatasource);virtual;
end;
...
...
constructor tquerythread.create(adoconnection:tadoconnection;adoquery:tadoquery;datasource:tdatasource);
begin
inherited create(true);
fadoconnection:=adoconnection;
fadoquery:=adoquery;
fdatasource:=datasource;
freeonterminate:=true;
resume;
end;
procedure tquerythread.execute;
begin
try
fadoquery.Open;
application.ProcessMessages;
//connectdatasource;
synchronize(connectdatasource);
except
end;
end;
procedure tquerythread.connectdatasource;
begin
fdatasource.dataset:=fadoquery;
end;
procedure tquerythread.showqryerror;
begin
application.ShowException(fqueryexception);
end;
procedure runbackgroundquery(adoconnection:tadoconnection;adoquery:tadoquery;datasource:tdatasource);
begin
tquerythread.create(adoconnection,adoquery,datasource);
end;