Page 1 of 1

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

Posted: Sat Sep 03, 2016 11:20 am
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

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

Posted: Sat Sep 03, 2016 2:59 pm
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).

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

Posted: Sat Sep 03, 2016 3:42 pm
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.

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

Posted: Sat Sep 03, 2016 4:52 pm
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.

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

Posted: Sat Sep 03, 2016 5:15 pm
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.

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

Posted: Sat Sep 03, 2016 6:52 pm
by IdeasVacuum
horse --> water :shock:

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

Posted: Sat Sep 03, 2016 8:39 pm
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.

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

Posted: Sat Sep 03, 2016 9:00 pm
by IdeasVacuum
We can't see what issues there might be because the code is not representative. Where is your CreateThread() for example?

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

Posted: Sun Sep 04, 2016 12:28 am
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

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

Posted: Mon Sep 05, 2016 7:36 am
by LiK137
Perfect, It runs smooth and as should.
ThanQ very much and problem solved