在写三层程序(使用SocketConnection与服务端连接)时有如下一段代码,
procedure TForm1.Button3Click(Sender: TObject);
var
  i : Shortint;
begin
  for i:=1 to 3 do begin
    ServerConnection.Connected:=True;
    ClientDataSet1.Data:=ServerConnection.AppServer.GetVendotList;
    ServerConnection.Connected:=False;
  end;
end;
当循环终值大于1时,程序会被异常终止,并弹出一对话框:
Debugger Exception Notification
Project TestProject.exe raised exception class 
Exception with message 'Access violation at address 
0048CCFA in module 'ScktSrvr4Hit.exe'. Read of address 00000000'. 
Process stopped. Use Step or Run to continue.
若将与服务端的连接写到循环之外,则程序可以正常运行,不会出现任何异常情况。

解决方案 »

  1.   

    在每次循环的结束时,加上Sleep(5000) 试一下。
    分析:for i:=1 to 3 do begin
        ServerConnection.Connected:=True;
        ClientDataSet1.Data:=ServerConnection.AppServer.GetVendotList;
        ServerConnection.Connected:=False;
      end;
    你这样等于在连续打开关闭连接。这样服务器端在不断的申请、释放资源。在资源还没释放时,又要申请,造成冲突。
    我没有测试,只是感觉这样。加上sleep试一下或者干脆在每次循环后加上一个:弹出对话框。试试,这样可以看出是不是由于关闭和打开之间没有时间间隔造成的。