Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Dec : Re: how to check connection?
| Subject: | Re: how to check connection? |
| Posted by: | "soonic" (xxsoon..@op.pl) |
| Date: | Fri, 28 Dec 2007 23:14:05 |
>
> You can't. Not accurately, anyway, because the OS itself can't do that
> reliably.
>
Well, then maybe you cen help me to solve the problem other way.
I have this procedure where I catch the problem with connection. My
application is a server and client at the same time.
'netunit.pas'
//var procedure DisconnectServer;
procedure TReadingThread.Execute;
begin
try
while not Terminated do
begin
FData.ReadFrom(FConn);
if not Terminated then Synchronize(UpdateGrid);
end;
except
// DisconnectServer;
Synchronize(DisconnectServer);
end;
end;
//procedure DisconnectServer;
procedure TReadingThread.DisconnectServer;
begin
with MainForm do
begin
//for server
if IdTCPServer.Active = False then exit;
IdTCPServer.Active:=False;
//for client
if ReadingThread <> nil then ReadingThread.Terminate;
if IdTCPClient.Connected then IdTCPClient.Disconnect;
end;
if ReadingThread <> nil then
begin
ReadingThread.WaitFor;
FreeAndNil(ReadingThread);
end;
end;
I have also a button which I can use to disconnect manually.
1) I call DisconnectServer in both ways: just DisconnectServer and
Synchronize(DisconnectServer);
I used always the first option until today. The problem I get, is that my
application hangs when I put DisconnectServer in clause try...except and
press manually disconnect button. If I add this procedure to TreadingThread
class and use synchronize then its ok (when I disconnect manually) because
there is condition if IdTCPServer.Active = False then exit;
2) Main problem. I use option with Synchronize(DisconnectServer). It fires
automatically when I unplug the network cable but my program freezes. Of
course it passes the condition IdTCPServer.Active = False then exit and then
freezes. I know that it hangs because I'm calling from
TReadingThread.Execute but how to solve this so it won't freeze?
s.