之前测试时使用全局变量ADOQUERY运行sql语句,由于sql语句执行时间很长,在一个客户端执行的期间,另一个客户端又要求执行这个指令,此时就返回错误:“在异步运行时,操作不能被执行。”这种情况在每个execute线程中建ADOQUERY局部变量就可以解决。但是由于sever端有一个memo1用来显示接收到的信息,同样道理,也有可能遇到上面使用这种共享资源而出错的情况。这时肯定不能建局部memo来解决啊?应该如何解决呢?排队?

解决方案 »

  1.   

    起线程,利用主进程执行vcl就可解决资源共享问题
      

  2.   

    线程同步方法,
    //------------------------------------------------------------------------------
    //显示通讯状态信息
    procedure TthdDP.DisplayInfo;
    var
      i:integer;
    begin
      MainForm.sgInfo.RowCount:=1;
      for i:=low(uData) to high(uData) do
      with MainForm do
      begin
        sgInfo.RowCount:=sgInfo.RowCount+1;
        sgInfo.Cells[0,sgInfo.RowCount-1]:=uData[i].uInfo.sIp;
        sgInfo.Cells[1,sgInfo.RowCount-1]:=uData[i].uInfo.sStatus;
        sgInfo.Cells[2,sgInfo.RowCount-1]:=dateTostr(uData[i].uInfo.dFrom);
      end;
      if MainForm.sgInfo.RowCount>1 then
        MainForm.sgInfo.fixedRows:=1;
    end;
    procedure TthdDP.Execute;
    begin
      inherited;
      case self.FOperType of
      1://暂存数据
        begin
          Synchronize(addRec);
          Synchronize(self.DisplayInfo);
        end;
      2://数据入库
        begin
          Synchronize(self.InsertDBReport);
          Synchronize(self.DeleteRec);
          Synchronize(DisplayData);
          Synchronize(DisplayInfo);
        end;
      3://连接请求
        begin
         Synchronize(self.initNewIPConn);
         Synchronize(DisplayInfo);
        end;
      4://client Disconnect
        begin
        end;
      end;
    end;
      

  3.   

    用Synchronize方法实现与VCL的同步http://lysoft.7u7.net
      

  4.   

    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);我使用的是idtcpserver,在他的excute事件中如何实现用Synchronize来向全局变量memo1中写字符串呢?因为不能传IdTCPServer1Execute事件中定义的局部变量进去AThread.Synchronize(Memo_addline)的Menthod中。也就是说应该在哪里定义这个Memo_addline?查询到的例子都是说新建一个thread,然后定义thread的局部变量,通过赋值将线程外的变量传给内部变量,再执行线程的menthod。但是IdTCPServer1Execute的参数AThread是内部已经建好的了,那应该如何处理呢?
    能否提供一些例子程序呢?谢谢!!
      

  5.   

    弄个互斥变量就ok了
    uses SyncObjs;
    FLock : TCriteronSection操作memo之前lock一下,完了解锁
    FLock.Enter;memo1.lines.add("""""");FLock.leave