IdTCPServer的Execute如何保存数据到数据库及操作文本文件,真蛋痛

CCC的 2011-11-22 07:20:21
procedure TServer.TcpSvrExecute(AContext: TIdContext);
begin
try
AContext.Connection.IOHandler.CheckForDataOnSource(50);
if not AContext.Connection.IOHandler.InputBufferIsEmpty then //检查是缓冲内是否有数据
begin
AContext.Connection.IOHandler.ReadTimeout := 3000;
if ReadPack(AContext,PackHead,RevBuf) then
begin
//收到包数量和发包数量增加
case PackHead.CMD of
xxx: begin
OpenDataSet(qry,sql,false);//--->执行这句有时会报错 qry为一个全局变量
DoBackUpData; //--->会出现I/O错误
end;
end;
except

end;
end;

procedure TServer.OpenDataSet(DataSet: TADOQuery; SqlStr: String; Flag: Boolean);
begin
with DataSet do
begin
Close;
Sql.Clear;
Sql.Add(SqlStr);
if Flag then
Open
else
ExecSQL;--》出错的地方
end;
end;

procedure TServer.DoBackUpData(fileName: string);
var
ReaderID,TagID,RecTime: string;
txtFile: TextFile;
Records: string;
begin
try
AssignFile(txtFile, fileName);
Reset(txtFile); //到这句报错I/O错误
while not eof(txtFile) do
begin
Readln(txtFile, Records);
//SplitRecords(Records,ReaderID,TagID,RecTime);
DoData(StrToInt(ReaderID),StrToInt(TagID),RecTime,0);
end;
finally
CloseFile(txtFile);
end;

在Execute的Case中加入临界区就没有问题,但是这样多线程都失去意义了,我改如何做呢
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Channels_net 2011-11-23
  • 打赏
  • 举报
回复
线程安全,老问题了,
CCC的 2011-11-22
  • 打赏
  • 举报
回复
我是不想用同步,这样效率就会下降,用同步的却没有问题,还有神马更好的方法灭有
kaikai_kk 2011-11-22
  • 打赏
  • 举报
回复
并没有同步啊!!!!
出错是肯定的

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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