Jump to content

ping on background?


gadelat
 Share

Recommended Posts

I have really unstable internet connection so i created script which will show me if i'm online or not. Problem is, that it waits for result of ping, so GUI is lagging... It's worse for me, because i have pings about 1000ms... So, is there any way to count ping time on background?

My example code:

#include <GUIConstantsEx.au3>

$mainwindow = GUICreate("Ping", 170, 20)
GUICtrlCreateLabel("You are:", 20, 5)
$status = GUICtrlCreateLabel("       ",100,5)
GUISetState(@SW_SHOW)

While 1
    $nmsg = GUIGetMsg()
    Switch $nmsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If Ping("www.google.com") > 0 Then
        GUICtrlSetData($status, "Online")
    Else
        GUICtrlSetData($status, "Offline")
    EndIf
WEnd
Link to comment
Share on other sites

One option when you need better control/speed over gui's is to use OnEventMode. Also you could lower the timeout on the Ping(), i can usually reach www.google.com in less than 80ms so 200 is more than enough as timeout for me.

Here is a quick edit off your script:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$mainwindow = GUICreate("Ping", 170, 20)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

GUICtrlCreateLabel("You are:", 20, 5)
$status = GUICtrlCreateLabel("     ",100,5)
GUISetState(@SW_SHOW)

While 1
    Sleep(750)
    If Ping("www.google.com", 200) > 0 Then
        GUICtrlSetData($status, "Online")
    Else
        GUICtrlSetData($status, "Offline")
    EndIf
WEnd

Func _Quit()
    Exit
EndFunc
Link to comment
Share on other sites

My pings are about 1000ms so i can't restrict ping timeout... It would get me offline everytime. And i don't think that GUIOnEventMode will help.

What do you mean with that? The default is 4000 so you could still set it at 2000 or so, and what do you mean about OnEventMode?? Using that will make everything respond directly, was that not what you wanted? You have to explain better what you want if that's not it. Edited by AdmiralAlkex
Link to comment
Share on other sites

It won't respond directly, until ping is in loop... Time of lag = respond time from ping, not timeout of ping, so restricting of ping timeout is useless.

1.)

>Ping on www.google.com will be 800ms

>Autoit is waiting 800ms

>Ping is returning value 800

>Result is online

2.)

If i restrict timeout to 1000ms:

>Ping on www.google.com will be 800ms

>Autoit is waiting 800ms

>Ping is returning value 800

>Result is online

3.)

If i restrict timeout to 200ms:

>Ping on www.google.com will be 800ms

>Autoit is waiting 200 ms and after that ping operation has been aborted.

>Ping is returning value 0

>Result is offline

Most important is truth of result and it isn't provided in example 3, but it's fastest. Example 2. is as fast as example 1, but it's faster if ping is above 1000ms (with wrong result). Example 1. is most reliable, so it's best way. Try to download 3 files from high speed servers and use this script, you will see about what GUI lagz i'm talking about.

Link to comment
Share on other sites

I am afraid I don't understand what you want, I don't see any lagging anywhere.

Perhaps we should take this from the beginning, please do this steps:

  • Post a small runnable reproducer. (if you have done anything since first post)
  • Tell us what is happening.
  • What is wrong?
  • How would you like it to work?
Link to comment
Share on other sites

I am afraid I don't understand what you want, I don't see any lagging anywhere.

Perhaps we should take this from the beginning, please do this steps:

  • Post a small runnable reproducer. (if you have done anything since first post)
  • Tell us what is happening.
  • What is wrong?
  • How would you like it to work?
Maybe the OP doesn't understand how the onEvent mode would work. Perhaps this example would help

#include <timers.au3>
AdlibEnable("doping", 2000)

Opt("GuiOnEventMode", 1)
$gui = GUICreate("test pings", 200, 200, 200, 200)
$btn = GUICtrlCreateButton("exit", 20, 20, 100, 20)
GUICtrlSetOnEvent(-1, "leaving")
$btn2 = GUICtrlCreateButton("Count up", 20, 60, 100, 20)
GUICtrlSetOnEvent(-1, "count")
$lab = GUICtrlCreateLabel("", 40, 100, 100, 20)
GUISetState()
Global $pinging = False, $online = True
While 1
    Sleep(40)
    
WEnd


Func leaving()
    Exit
EndFunc ;==>leaving

Func Count()
    Local $c = Number(GUICtrlRead($lab))
    GUICtrlSetData($lab, $c + 1)
EndFunc ;==>Count


Func doping()
    ConsoleWrite("doing the ping thing" & @CRLF)
    $pinging = True
    $online = Ping("216.239.59.99", 2000)
    ConsoleWrite($online & @CRLF)
    
    If $online = 0 Then
        MsgBox(262144, "ERROR", "No internet connection!")
        Exit
    EndIf
    $pinging = False
EndFunc ;==>doping

Clicking the count up button will show the responsiveness of the script even though the ping is going on at the same time.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I assume a dllcall would be faster then pinging a server but I'm not sure if it would work the same way. Anyways you can always try this and see if it helps.

DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)

Link to comment
Share on other sites

I am afraid I don't understand what you want, I don't see any lagging anywhere.

You don't see any lagging because you have low ping. You have ping on google about 80 ms so gui is freezed in 80ms, but i have ping 800ms so i can really feel lagging. Controls like gui buttons or buttons in title bar (minimize, maximize, close ) don't react on mouse move or mouse click immediately, only after returning ping...

Maybe the OP doesn't understand how the onEvent mode would work. Perhaps this example would help

Thanks, it helped because i didn't know about AdlibEnable function, but onEvent really don't help. Now is my gui only lagging every 2000ms, so i think i can say that problem is solved, but I wish to remove lags completely.

Here is my script with better reaction times than in my first post (thanks to AdlibEnable) without onEvent mode

#include <GUIConstantsEx.au3>
AdlibEnable("doping", 2000)
$mainwindow = GUICreate("Ping", 170, 20)
GUICtrlCreateLabel("You are:", 20, 5)
$status = GUICtrlCreateLabel("       ",100,5)
GUISetState(@SW_SHOW)

While 1
    $nmsg = GUIGetMsg()
    If $nmsg = $GUI_EVENT_CLOSE Then
        Exit
    EndIf
   
WEnd

Func doping()
     If Ping("www.google.com") > 0 Then
        GUICtrlSetData($status, "Online")
    Else
        GUICtrlSetData($status, "Offline")
    EndIf
EndFunc

I assume a dllcall would be faster then pinging a server but I'm not sure if it would work the same way. Anyways you can always try this and see if it helps.

DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)

I don't know how can i use it, MsgBox(0, "", DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)) don't return anything, if you can, please please post example.
Link to comment
Share on other sites

OK, i added the InterNetGetConnectedState:

#include <GUIConstantsEx.au3>
AdlibEnable("doping", 2000)
Opt("GUIOnEventMode", 1)

$mainwindow = GUICreate("Ping", 170, 20)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

GUICtrlCreateLabel("You are:", 20, 5)
$status = GUICtrlCreateLabel("       ",100,5)
GUISetState()

While 1
    Sleep(40)
WEnd

Func doping()
    Local $online =  DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)
   
    If Not IsArray($online) Then
        GUICtrlSetData($status, "UnKnown")
    ElseIf $online[0] = True Then
        GUICtrlSetData($status, "Online")
    Else
        GUICtrlSetData($status, "Offline")
    EndIf
    $pinging = False
EndFunc ;==>doping

Func _Quit()
    Exit
EndFunc

Func leaving()
    Exit
EndFunc ;==>leaving

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

You don't see any lagging because you have low ping. You have ping on google about 80 ms so gui is freezed in 80ms, but i have ping 800ms so i can really feel lagging. Controls like gui buttons or buttons in title bar (minimize, maximize, close ) don't react on mouse move or mouse click immediately, only after returning ping...

Thanks, it helped because i didn't know about AdlibEnable function, but onEvent really don't help. Now is my gui only lagging every 2000ms, so i think i can say that problem is solved, but I wish to remove lags completely.

Why do you refuse to accept the truth? Both me and martin has showed you what do to, use OnEventMode! When using OnEventMode the buttons etc that you press are executed instantly and then the script return to what it did previously. If you try the scripts by me and martin you will see that they are LAG-FREE!!
Link to comment
Share on other sites

Why do you refuse to accept the truth? Both me and martin has showed you what do to, use OnEventMode! When using OnEventMode the buttons etc that you press are executed instantly and then the script return to what it did previously. If you try the scripts by me and martin you will see that they are LAG-FREE!!

I'm using
Link to comment
Share on other sites

I'm using

I think you could have problems with the script you showed which I took care to avoid in my example. You are calling the function with AdlibEnable every 2 seconds. It might not be needed to be called that often, but the point is that ping could take up to 4 seconds to execute. If it repeatedly operates successfully but takes longer than 2 seconds then you will end up with more and more copies of the function runnning at the same time and eventually something will go wrong. So you need to change adlibEnable to operate every 4 seconds or more, or change the default maximum time for ping to be less than the ADlibEnable time, or you need to do the sort of thing I did to avoid running the function multiple times.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Here is the way I do it in Project Express. It checks the net connection every 5 minutes. and sets the net dependent controls accordingly. Using ping will cause a lond startup delay if you don't have a net connection or if the site you are pinging is having problems. Thats why the DLL call is much better.

If MOD(@Min, 5) = 0 And @Sec < 2 Then
      _NetStatus()
   EndIf

Func _NetStatus()
  $cStatus = DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int_ptr", 0, "int", 0)
  If NOT $cStatus[0] Then
    For $i = $Lbl_Search To $Lbl_sTnet
      If GUICtrlGetState($i) < 144 Then GUICtrlSetState($i, 144)
    Next
    If GUICtrlGetState($Menu_Tools_Translate) < 144 Then GUICtrlSetState($Menu_Tools_Translate, 144)
    If GUICtrlGetState($Menu_Ref_RegXp_Tut) < 144 Then GUICtrlSetState($Menu_Ref_RegXp_Tut, 144)
    If GUICtrlGetState($Menu_Ref_RegXp_Msdn) < 144 Then GUICtrlSetState($Menu_Ref_RegXp_Msdn, 144)
    If GUICtrlGetState($Menu_Udate) < 144 Then GUICtrlSetState($Menu_Udate, 144)
  Else
    For $i = $Lbl_Search To $Lbl_sTnet
      If GUICtrlGetState($i) > 80 Then GUICtrlSetState($i, 80)
    Next
    If GUICtrlGetState($Menu_Tools_Translate) <> 80 Then GUICtrlSetState($Menu_Tools_Translate, 80)
    If GUICtrlGetState($Menu_Ref_RegXp_Tut) <> 80 Then GUICtrlSetState($Menu_Ref_RegXp_Tut, 80)
    If GUICtrlGetState($Menu_Ref_RegXp_Msdn) <> 80 Then GUICtrlSetState($Menu_Ref_RegXp_Msdn, 80)
    If GUICtrlGetState($Menu_Udate) <> 80 Then GUICtrlSetState($Menu_Udate, 80)
  EndIf
EndFunc  ;<===> _NetStatus()

EDIT: I should also mention that the function gets called the first time just before the GUI is shown

_NetStatus()

GUISetState()

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

OK, i added the InterNetGetConnectedState

Should be so:

Global Const $INTERNET_CONNECTION_MODEM = 0x1
Global Const $INTERNET_CONNECTION_LAN = 0x2
Global Const $INTERNET_CONNECTION_PROXY = 0x4
Global Const $INTERNET_CONNECTION_MODEM_BUSY = 0x8
Global Const $INTERNET_RAS_INSTALLED = 0x10
Global Const $INTERNET_CONNECTION_OFFLINE = 0x20
Global Const $INTERNET_CONNECTION_CONFIGURED = 0x40

Dim $State, $val

$aRet = DllCall("wininet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)

$val = $aRet[1]

If BitAND($val, $INTERNET_CONNECTION_MODEM) Then $State &= "Modem connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_LAN) Then $State &= "LAN connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_PROXY) Then $State &= "Proxy connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_MODEM_BUSY) Then $State &= "Modem bussy" & @LF
If BitAND($val, $INTERNET_RAS_INSTALLED) Then $State &= "RAS installed" & @LF
If BitAND($val, $INTERNET_CONNECTION_OFFLINE) Then $State &= "Offline connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_CONFIGURED) Then $State &= "Connection configured"

MsgBox(0, "Connection", $State)

If StringInStr($State, "Modem connection") Then ConsoleWrite("!> Online" & @LF)

:P

Edited by rasim
Link to comment
Share on other sites

Should be so:

Global Const $INTERNET_CONNECTION_MODEM = 0x1
Global Const $INTERNET_CONNECTION_LAN = 0x2
Global Const $INTERNET_CONNECTION_PROXY = 0x4
Global Const $INTERNET_CONNECTION_MODEM_BUSY = 0x8
Global Const $INTERNET_RAS_INSTALLED = 0x10
Global Const $INTERNET_CONNECTION_OFFLINE = 0x20
Global Const $INTERNET_CONNECTION_CONFIGURED = 0x40

Dim $State, $val

$aRet = DllCall("wininet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)

$val = $aRet[1]

If BitAND($val, $INTERNET_CONNECTION_MODEM) Then $State &= "Modem connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_LAN) Then $State &= "LAN connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_PROXY) Then $State &= "Proxy connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_MODEM_BUSY) Then $State &= "Modem bussy" & @LF
If BitAND($val, $INTERNET_RAS_INSTALLED) Then $State &= "RAS installed" & @LF
If BitAND($val, $INTERNET_CONNECTION_OFFLINE) Then $State &= "Offline connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_CONFIGURED) Then $State &= "Connection configured"

MsgBox(0, "Connection", $State)

If StringInStr($State, "Modem connection") Then ConsoleWrite("!> Online" & @LF)

:P

I thought that he just wanted to know if he had a connection, not the type of connection.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

How about just a little tray icon that changes. This script checks every 5 secs and set the tray icon. Mouse over icon for online or offline info.

#region Tray menu setup
Opt("TrayMenuMode",1)
Opt("TrayOnEventMode",1)
#NoTrayIcon

$tray_exit   = TrayCreateItem("Exit")
TrayItemSetOnEvent($tray_exit, "_Quit")
TraySetState()
#endregion Tray menu setup

While 1
    $MyConnect=DllCall("WinInet.dll","int","InternetGetConnectedState","int",0,"int",0)
    If $MyConnect[0] = 0 Then
        TraySetIcon("Shell32.dll",200)
        TraySetToolTip("Offline at "&@HOUR&":"&@MIN&":"&@SEC)
    Else
        TraySetIcon("Shell32.dll",15)
        TraySetToolTip("Online at "&@HOUR&":"&@MIN&":"&@SEC)
    EndIf
    sleep(5000)
WEnd

Func _Quit()
    Exit
EndFunc
Link to comment
Share on other sites

This does work if connection exists:

If StringInStr($State, "Modem connection") Then ConsoleWrite("!> Online" & @LF)

:P

Sure but why do you need it when using array element[0] on the dll call returns the connected state regardless of type?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Sorry for my previous short post, when i sent it, autoit forum server got out of order. I wanted to say that i'am using onEvent mode, but speed is same as in MessageLoop, i tested it. Below is code which i'm using now. It works fine when ping is under 400-500ms, but after that i really feel lagging (every 2sec). Maybe is martin in #13 right and it needs only changing time in AdlibEnable, but i think while is AdlibEnable function executing, it pauses the script and this is written in autoit help too. So if i change time in adlibenable to 4000ms, it will only lagging every 4sec. However, i can't test it now, because i am home this weekend and here're pings ~40ms so speed of script is very good.

#include <GUIConstantsEx.au3>

AdlibEnable("doping", 2000)
Opt("GUIOnEventMode", 1)

$mainwindow = GUICreate("lamp", 170, 25)
GUISetOnEvent($GUI_EVENT_CLOSE, "_quit")
GUICtrlCreateLabel("Internet connection:", 40, 5)
$net = GUICtrlCreateGraphic(145,7,20,10)
GUICtrlSetGraphic($net, $GUI_GR_ELLIPSE, 0,0, 11,11)
GUISetState(@SW_SHOW)
While 1
    Sleep(40)
WEnd

Func doping()
    ConsoleWrite("doing the ping thing" & @CRLF)
    $online = Ping("216.239.59.99")
    ConsoleWrite($online & @CRLF)
    
    If $online > 0 Then
        GUICtrlSetGraphic($net, $GUI_GR_COLOR, 0x00ff00, 0x00ff00)
        GUICtrlSetGraphic($net, $GUI_GR_ELLIPSE, 0,0, 10,10)
        GUICtrlSetGraphic($net, $GUI_GR_REFRESH)
    Else
        GUICtrlSetGraphic($net, $GUI_GR_COLOR, 0xff0000, 0xff0000)
        GUICtrlSetGraphic($net, $GUI_GR_ELLIPSE, 0,0, 10,10)
        GUICtrlSetGraphic($net, $GUI_GR_REFRESH)
    EndIf
EndFunc

Func _quit()
    Exit
EndFunc

I tryed dllcall too (examples in #10, #15 and #17, #14 i couldn't implement), but it looks like it shows status of my network adapter, but i need verify connection to any server on internet.

Edited by gadelat
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...