我在做语音叫号的程序,要不停的检测在A表中有没有新的数据,如果有新的数据就把他取出来呼叫然后在通过串口显示在LED上,使用方法是用了两个线程,一个是不停的刷新表里的数据,另一个是把数据取出来呼叫并显示,结果现在就算没数据时候,只检测数据CPU就到了50%,而且在50%检测的时候程序就和死掉一样,谁能解释一下,并写些简单的控制代码
解决方案 »
- 简单的SUCC?
- delphi2005能调用delphi7做出来的bpl
- 如何动态设定一个STRING类型的二维数组长度
- 请推荐网络编程的入门书籍,谢了.
- 内存释放的问题吗?
- 如何将UNIX下生成的16进制文件转换为文本文件??
- Some questions about a project
- 用過dxDBGrid和dxComponentPrinter的朋友請進,如何用這二個控件進行打印時打印出總匯總和按頁匯總
- 请教一个关于EXCEL的问题(谁知道?)
- 请问:TTable组件如何实现自动LOGIN数据库?
- 在客户那从早上9点泡到下午4点 早饭晚饭都没吃 还不觉得饿 散一分
- case of 后的问题
为什么呢,很简单,因为如果用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;