Check Host Alive freezes app for more than 3K msec [SOLVED]

Windows specific forum
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Check Host Alive freezes app for more than 3K msec [SOLVED]

Post by LiK137 »

In my program I am periodically check for host availability.
Previously I was using this check inside main procedure and it was taking more than 15000 msec.
Then I moved this checking to thread then it dropped to less than 3000 msec but even this is in a thread it causes delay in main procedure and app.

Here is the code sample:

Code: Select all

Procedure.b xPing(xIP.s,xPort)
  Protected chkIP, PingRet.b
    chkIP = OpenNetworkConnection(xIP.s,xPort,#PB_Network_TCP|#PB_Network_IPv4,100) ; havChekdDiffTimoutz
    If chkIP
      CloseNetworkConnection(chkIP)
      ProcedureReturn #True
    Else
      ProcedureReturn #False
    EndIf
EndProcedure

 Procedure xPingStaThread(Dummy.b)
   Shared xPingStat.b
   Protected tmp.l
   DebugString("ThreadStarting")
   Repeat
     pingskip+1
     tmp.l=ElapsedMilliseconds()
     If pingskip=1
       If xPing(#NAServer,21)
         xPingStat=#True
       Else
         xPingStat=#False
       EndIf
     ElseIf pingskip>=600  
       pingskip=0
     EndIf  
     tmp=ElapsedMilliseconds()-tmp.l
     If tmp>13
       DebugString(Str(tmp)+" AFISThreadedStat: "+Str(xPingStat))
     EndIf  
     tmp=0
     Delay(100)
   ForEver  
 EndProcedure  

Procedure Main()
Shared xPingStat.b
Repeat
SkipStat+1
;SomeCodeHere
  If SkipStat=1
    If xPingStat=#True
      ;SomeCodeHere
    Else
      ;SomeCodeHere
    EndIf
  ElseIf SkipStat>=600
    SkipStat=0
  EndIf
  Delay(100)
Forever
EndProcedure


;SomeCodeHere
Main()
;SomeCodeHere
Last edited by LiK137 on Tue Jan 10, 2017 10:37 am, edited 2 times in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Check Host Alive freezes app for more than 3000 msec

Post by IdeasVacuum »

....your thread should do all the work and not call a Procedure outside. Ensure that the thread Procedure and the initialisation of that Procedure are applied as per the Help. You cannot rely on ElapsedMilliseconds() for accurate timing (see Help).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Check Host Alive freezes app for more than 3000 msec

Post by LiK137 »

This delays I mentioned is not just but really causes window freezing. With ElapsedMilliseconds() I get these delays of pinging and these delays is exactly how long the main window stays not responding. I could keep the connection alive as an alternative way and sending packets to host and get HostStatus True or False according to receiving answer from Host but for that I have to make hosts listening my connection. I want to find, there must be easy way which most applications (Skype, TeamViewer and etc) using.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Check Host Alive freezes app for more than 3000 msec

Post by IdeasVacuum »

Your call to a Procedure outside of the thread can cause the app to freeze. Like I said, do all the check host work inside the thread - that way, if the check freezes, your app doesn't.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Check Host Alive freezes app for more than 3000 msec

Post by LiK137 »

Code: Select all

Procedure xPingStaThread(Dummy.b)
   Shared xPingStat.b
   Protected chkIP, tmp.l
   Repeat
     pingskip+1
     tmp.l=ElapsedMilliseconds()
     If pingskip=1
       chkIP = OpenNetworkConnection(#NAServer,21,#PB_Network_TCP|#PB_Network_IPv4,100)
       If chkIP
         CloseNetworkConnection(chkIP)
         xPingStat=#True
       Else
         xPingStat=#False
       EndIf
     ElseIf pingskip>=600  
       pingskip=0
     EndIf  
     tmp=ElapsedMilliseconds()-tmp.l
     If tmp>13
       DebugString(Str(tmp)+" AFISThreadedStat: "+Str(xPingStat))
     EndIf  
     tmp=0
     Delay(100)
   ForEver  
 EndProcedure  

The same, no change, freezes for 3083 msec.
So when I disable/enable network, in time when hostcheck occurs the window responds after 3 seconds.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Check Host Alive freezes app for more than 3000 msec

Post by IdeasVacuum »

horse --> water :shock:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Check Host Alive freezes app for more than 3000 msec

Post by LiK137 »

ThanQ very much but You better try to replay the problem.
It is very annoying little problem which do not make my app any big inconvenience but little do.
So very big thanx to everyone who may give hint, reply or solution.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Check Host Alive freezes app for more than 3000 msec

Post by IdeasVacuum »

We can't see what issues there might be because the code is not representative. Where is your CreateThread() for example?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Check Host Alive freezes app for more than 3000 msec

Post by IdeasVacuum »

A snippet:

Code: Select all

InitNetwork()

Structure Srvr
sSrvrName.s ;Url or IP string
iSrvrPort.i
EndStructure

Global igConnect.i = 0

Procedure CheckServer(*Server.Srvr)
;#---------------------------------
;Thread is timed-out after approx 3 secs if connection fails
Protected iFinishTime.i, iElapsedTime.i
Protected iStartTime.i = ElapsedMilliseconds()

              Repeat
                       igConnect = OpenNetworkConnection(*Server\sSrvrName, *Server\iSrvrPort)
                    If igConnect : CloseNetworkConnection(igConnect) : Break : EndIf

                           iFinishTime = ElapsedMilliseconds()
                          iElapsedTime = (iFinishTime - iStartTime)
                       If(iElapsedTime > 3000) : Break : EndIf
              ForEver
EndProcedure



Procedure MyMain()
;#----------------
Protected   iThrd.i
Protected *Server.Srvr = AllocateMemory(SizeOf(Srvr))
     *Server\sSrvrName = "ptbtime2.ptb.de"
     *Server\iSrvrPort = 37
                 iThrd = CreateThread(@CheckServer(), *Server)

              Delay(100) : MessageRequester("Server State", Str(igConnect)) ; <> 0

EndProcedure

MyMain()
End
If you want to continually check the server until a connection is made, you could change the loop such that it is dependent on the Global igConnect. The thread could be stopped/abandoned by changing that var if it did not stop itself:

Code: Select all

InitNetwork()

Structure Srvr
sSrvrName.s ;Url or IP string
iSrvrPort.i
EndStructure

Global igConnect.i = 0

Procedure CheckServer(*Server.Srvr)
;#---------------------------------
;Thread stops on connection success

              While(igConnect = 0)

                       igConnect = OpenNetworkConnection(*Server\sSrvrName, *Server\iSrvrPort)
                    If igConnect : CloseNetworkConnection(igConnect) : Break : EndIf
              Wend
EndProcedure



Procedure MyMain()
;#----------------
Protected   iThrd.i
Protected *Server.Srvr = AllocateMemory(SizeOf(Srvr))
     *Server\sSrvrName = "ptbtime2.ptb.de"
     *Server\iSrvrPort = 37
             igConnect = 0
                 iThrd = CreateThread(@CheckServer(), *Server)

              Delay(100) : MessageRequester("Server State", Str(igConnect)) ; <> 0

             ;Abandon Thread
             If(igConnect = 0) : igConnect = -1 : Delay(100) : EndIf

             Delay(100) : MessageRequester("Thread State", Str(IsThread(iThrd))) ; <> 0 if thread still running
EndProcedure

MyMain()
End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Check Host Alive freezes app for more than 3000 msec

Post by LiK137 »

Perfect, It runs smooth and as should.
ThanQ very much and problem solved
Post Reply