使用idtcpserver,设置了全局变量online,一个个下线时正常,但是同时下线时出错,只能下线一个,代码如下:procedure Tmain.IdTCPServer1Execute(AThread: TIdPeerThread);
 var
 i:integer;
 msg:RmessageData;
 rec:RrectiveData;
begin
  
 athread.Connection.ReadBuffer(msg,sizeof(msg));
if msg.command='offOnline' then
  begin
   EnterCriticalSection(cs);
   try
     tmpmsg:=msg;//全局变量,将msg传给Synchronize(offonline);
     for i:=0 to high(online) do  //删除下线用户的联机节点
       begin
         if msg.userName=online[i].userName then
           begin
             online[i]:=online[high(online)];
             setlength(online,high(online));
             break;
           end;
       end;
    
     for i:=0 to high(online) do   //发送下线用户的用户名
       begin
         rec.command:='deleOnline';
         rec.userName:=msg.userName;
         online[i].AThread.Connection.WriteBuffer(rec,sizeof(rec));
       end;
     finally
       LeaveCriticalSection(cs);
     end;
     athread.Synchronize(offonline);   end;

解决方案 »

  1.   

    经过测试,问题出在这段代码上:
    for i:=0 to high(online) do   //发送下线用户的用户名
           begin
             rec.command:='deleOnline';
             rec.userName:=msg.userName;
             online[i].AThread.Connection.WriteBuffer(rec,sizeof(rec));
           end;
    大家帮我看下出错的原因是什么,要怎样改才可以?
      

  2.   

    问题已解决,改后代码如下:
     if msg.command = 'offOnline' then   //客户端下线事件
      begin
        cs.Acquire;
        try
          for i := 0 to high(online) do  //删除下线用户的联机节点
          begin
            if msg.userName = online[i].userName then
            begin
              online[i] := online[high(online)];
              setlength(online,high(online));
              break;
            end;
          end;
        finally
          cs.Release;
        end;
        cs.Acquire;
        try
          for i := 0 to listview1.Items.Count-1 do
          begin
            if listview1.Items.Item[i].Caption = msg.userName then
            begin
              listview1.Items.Item[i].Delete;
              break;
            end;
          end;
          memo1.Lines.Add('用户 '+msg.userName+' 下线了');
        finally
          cs.Release;
        end;
        for i := 0 to high(online) do   //发送下线用户的用户名
        begin
          rec.command := 'deleOnline';
          rec.userName := msg.userName;
          online[i].AThread.Connection.WriteBuffer(rec,sizeof(rec));     end;
      end;