+ -
当前位置:首页 → 问答吧 → IdTCPServer的Execute如何保存数据到数据库及操作文本文件,真蛋痛

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

时间:2011-11-22

来源:互联网

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中加入临界区就没有问题,但是这样多线程都失去意义了,我改如何做呢

作者: hnlg81004   发布时间: 2011-11-22

并没有同步啊!!!!
出错是肯定的

作者: kaikai_kk   发布时间: 2011-11-22

我是不想用同步,这样效率就会下降,用同步的却没有问题,还有神马更好的方法灭有

作者: hnlg81004   发布时间: 2011-11-22