发现执行以下的代码就会使SQL-SERVER的CPU占用到50%还有一个线程也是大概和这个写的差不多执行就没有问题 procedure displaythread.autonext; begin With dm.ADOQuery4 do begin Close; SQL.Clear; SQL.Add('exec auto_doct_next'); execsql; end; end;constructor displaythread.create(abc:boolean); begin inherited create(false); end;procedure displaythread.Execute; begin while (not terminated) do begin sleep(10000); synchronize(autonext); end; end;
为什么呢,很简单,因为如果用TIMER的话,那我们假设如果数据量很大,在30秒里都无法处理完插入的动作话,那么在下一次的时候,很容易出现死锁.
如果是同一台,那赶紧换,升级内存,我们就是这样解决了。
procedure displaythread.autonext;
begin
With dm.ADOQuery4 do
begin
Close;
SQL.Clear;
SQL.Add('exec auto_doct_next');
execsql;
end;
end;constructor displaythread.create(abc:boolean);
begin
inherited create(false);
end;procedure displaythread.Execute;
begin
while (not terminated) do
begin
sleep(10000);
synchronize(autonext);
end;
end;
...
private
IsOleInitialized:Boolean;
SQLText : String;
Connection : TADOConnection;
Command : TADOCommand;//象楼主这样使用存储过程,还可以使用TADOStoredProc
...
protected
procedure Execute;override;
public
constructor Create( CreateSuspended : Boolean
; ConnectionString : String
; SQLString : String);
destructor Destroy;override;
end;构造函数constructor TDisplayThread.Create( CreateSuspended : Boolean
; ConnectionString : String
; SQLString : String);
begin
Inherited Create(True);//不管是否一开始就需要运行,先暂停(基本要素还没实例化成功)
IsOleInitialized := False;//要使用ADO,就需要进行COM初始化
Connection := TADOConnection.Create(Nil);//注意这个Connect的Owner参数传空值
Command := TADOCommand.Create(Nil);
Connection.ConnectionString := ConnectionString;
Command.Connection := Connection;
Command.CommandText := 'select ''Test Connection...''';//这是MSSQL可用的测试,不同数据库换之
try
Command.Execute;
except
//数据库连接失败,这里处理,通常需要抛出异常
end;
SQLText := SQLString;
if Not CreateSuspended then
Resume;//如果创建即需执行,就执行起来吧
end;destructor TDisplayThread.Destroy;
begin
Command.Free;
Connection.Free;
if IsOleInitialized then
CoUninitialize;//线程还是有可能会被意外结束的,所以这里加一个保险
end;
线程函数procedure TDisplayThread.Execute;
begin
CoInitializeEx(nil, COINIT_MULTITHREADED);//COM初始化
IsOleInitialized := true;
try
Command.Text := SQLText;
while Not Terminated do
begin
try
Command.Execute;
except
on E:Exception do
begin
LogException(E);
end;
end;
Sleep(10000);
end;
finally
CoUninitialize;//COM调用结束
end;end;