https://autohotkey.com/boards/viewtopic ... 040#p64040
[Update 2] - (uses exitcode /random exitcode instead random file):
https://autohotkey.com/boards/viewtopic.php?f=6&t=12399
Update 1 and Update 2 are not reliable, they use ping cmd! (Using cmd.exe or ping.exe to check internet connection is not reliable at all, see why in [Update 2]!)
[Update 3] - (DllCall and Com Ping Version - Seems to be reliable):
https://autohotkey.com/boards/viewtopic ... 440#p64440
Update 3 is the most reliable! (DllCall version particularly! It is the most reliable, safe and faster!)
Check every 10 seconds:
(Ignore the notes below "guiclose")
Code: Select all
gui, add, text, x65 y20, Status:
gui, add, text, x+5 vStatus, Please Wait! Checking for Internet Connection!
gui, add, text, x65 y+20, Last Check:
gui, add, text, x+5 w150 vLastCheck, Wait ...!
gui, add, text, x65 y+20, (Check Internet Connection every 10 seconds!)
gui, add, text, x70 y+20, by, InFiLLion
gui, show, w400 h150, Internet Connection Checker!
InternetCheck:
guicontrol, , Status, Please Wait! Checking for Internet Connection!
PingFileName = %a_Sec%%a_MSec% ;%a_Sec% current windows os seconds
;%a_MSec% current windows os milliseconds
;for every internet check, the file name will be diferrent (randomized)
run, cmd /c ipconfig /flushdns && ping www.google.com && Type Nul > "%a_workingdir%\%PingFileName%", , hide UseErrorLevel, PingPid
;"ping www.google.com" ping 4 times
;"ping -n 1 www.google.com" ping 1 time
;"ping -n 10 www.google.com" ping 10 times
;"%a_workingdir%\%PingFileName%" must use quotation marks (""), otherwise, if path contains "space" character, etc, errors may happen
if ErrorLevel ;if "cmd.exe" or "ping.exe" files are not found
{
msgbox, Fail! "cmd.exe" or "ping.exe" files not found!
exitapp
}
settimer, CheckPingFileName, -10000 ;"-10.000" check once after 10 seconds
return
CheckPingFileName:
IfExist, %A_WorkingDir%\%PingFileName% ;if %PingFileName% exist in script "working dir"
{
FileDelete, %A_WorkingDir%\%PingFileName%
guicontrol, , Status, Wait 10 seconds ...!
guicontrol, , LastCheck, Success - Connected!
settimer, InternetCheck, -10000 ;"-10000"after 10 seconds, "InternetCheck" lable will be executed only once
}
else
{
guicontrol, , Status, Wait 10 seconds ...!
guicontrol, , LastCheck, Fail - Not Connected!
settimer, InternetCheck, -10000 ;"-10000"after 10 seconds, "InternetCheck" lable will be executed only once
}
return
guiclose:
process, close, %PingPid% ;close "cmd.exe" executed by autohotkey through its unique process id (pid) if process still exist
process, close, ping.exe ;close "ping.exe" executed by %PingPid% if process still exist
FileDelete, %A_WorkingDir%\%PingFileName%
exitapp
;"&&" if "ipconfig /flushdns" succeed, "ping www.google.com" will be executed, if ping succeed, "Type Nul > %a_workingdir%\%PingFileName%" will be executed
;"&" if "ipconfig /flushdns" fails, "ping www.google.com" will be executed anyway
;"||" if "ping www.google.com" doesn't succeed (if google ping fails), "exit" will be executed
;if "ipconfig /flushdns" fails, "ping www.google.com" will not be executed and google ping will be considered as a failure, so, "Type Nul > %a_workingdir%\%PingFileName%" will not be executed
;"ipconfig /flushdns" neccessary because, if internet goes down, dns cache will be refreshed, so ping to goolge will fail immediately
;"Type Nul > %a_workingdir%\%PingFileName%" if ping to google succeed, file named "%PingFileName%" will be created at script "working directory"
;"hide" cmd windows will not show up (will be hidden)
;"UseErrorLevel" if "cmd.exe" or "ping.exe" is not found, autohotkey will use "ErrorLevel" instead showing a default "warning message box" and then stop lines bellow execution!
;by using "ErrorLevel", the programmer will decide what will happen if file not found (in this case "cmd.exe" or "ping.exe" )
;"PingPid" stores "cmd.exe" unique process id (Pid) executed by autohotkey in "PingPid" variable! ("PingPid" coud be any other name, for exampe, "PidPing, etc")
;"/k" prevents cmd from closing after ping process, if "exit" command line is not used (if "pause" command line is used, cmd will not exit after pressing any keyboard button, unless "exit" command line is used)
;"/k" command lines are shown in cmd title windows - example: "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")"
;"/c" instead "/k" , cmd will exit after ping process, (if "pause" command line is used, cmd will exit after pressing any keyboard button)
;"/c" command lines are not shown in cmd title windows - example: no "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")" will appear
; without "/k" or "/c" , cmd will open, but the "batchcode" lines will not be executed
/* ;notes....................................................... ;commands from the right and bellow /* will not be executed ..............................................................................................
& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.
&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
| [...] means that all commands will run at same time, but if one command fails to open, all command will fail too regardless their position (example):
notepad | mspaint | control
if notepad fails, all will fail too ; if control fails, all will fail too ; if mspaint fails, all will fail too
|| [...] command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
( ) [...] (command1 & command2)
Use to group or nest multiple commands.
; or , command1 parameter1;parameter2
Use to separate command parameters.
*/ ;notes....................................................... ;commands from the right and bellow */ will be executed ............................................................................................
Code: Select all
gui, add, text, x90 y65, Please Wait! Checking for Internet Connection!
gui, show, w400 h150, Checking Internet Connection ...!
PingFileName = %a_Sec%%a_MSec% ;%a_Sec% current windows os seconds
;%a_MSec% current windows os milliseconds
;for every internet check, the file name will be diferrent (randomized)
run, cmd /c ipconfig /flushdns && ping www.google.com && Type Nul > "%a_workingdir%\%PingFileName%", , hide UseErrorLevel, PingPid
;"ping www.google.com" ping 4 times
;"ping -n 1 www.google.com" ping 1 time
;"ping -n 10 www.google.com" ping 10 times
;"%a_workingdir%\%PingFileName%" must use quotation marks (""), otherwise, if path contains "space" character, etc, errors may happen
if ErrorLevel ;if "cmd.exe" or "ping.exe" files are not found
{
msgbox, Fail! "cmd.exe" or "ping.exe" files not found!
exitapp
}
settimer, CheckPingFileName, -10000 ;"-10.000" check once after 10 seconds
return
CheckPingFileName:
gui, destroy
IfExist, %A_WorkingDir%\%PingFileName% ;if %PingFileName% exist in script "working dir"
{
msgbox, Sucess! Connected!
}
else
msgbox, Fail! Not Connected!
FileDelete, %A_WorkingDir%\%PingFileName%
exitapp
guiclose:
process, close, %PingPid% ;close "cmd.exe" executed by autohotkey through its unique process id (pid) if process still exist
process, close, ping.exe ;close "ping.exe" executed by %PingPid% if process still exist
FileDelete, %A_WorkingDir%\%PingFileName%
exitapp
;"&&" if "ipconfig /flushdns" succeed, "ping www.google.com" will be executed, if ping succeed, "Type Nul > %a_workingdir%\%PingFileName%" will be executed
;"&" if "ipconfig /flushdns" fails, "ping www.google.com" will be executed anyway
;"||" if "ping www.google.com" doesn't succeed (if google ping fails), "exit" will be executed
;if "ipconfig /flushdns" fails, "ping www.google.com" will not be executed and google ping will be considered as a failure, so, "Type Nul > %a_workingdir%\%PingFileName%" will not be executed
;"ipconfig /flushdns" neccessary because, if internet goes down, dns cache will be refreshed, so ping to goolge will fail immediately
;"Type Nul > %a_workingdir%\%PingFileName%" if ping to google succeed, file named "%PingFileName%" will be created at script "working directory"
;"hide" cmd windows will not show up (will be hidden)
;"UseErrorLevel" if "cmd.exe" or "ping.exe" is not found, autohotkey will use "ErrorLevel" instead showing a default "warning message box" and then stop lines bellow execution!
;by using "ErrorLevel", the programmer will decide what will happen if file not found (in this case "cmd.exe" or "ping.exe" )
;"PingPid" stores "cmd.exe" unique process id (Pid) executed by autohotkey in "PingPid" variable! ("PingPid" coud be any other name, for exampe, "PidPing, etc")
;"/k" prevents cmd from closing after ping process, if "exit" command line is not used (if "pause" command line is used, cmd will not exit after pressing any keyboard button, unless "exit" command line is used)
;"/k" command lines are shown in cmd title windows - example: "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")"
;"/c" instead "/k" , cmd will exit after ping process, (if "pause" command line is used, cmd will exit after pressing any keyboard button)
;"/c" command lines are not shown in cmd title windows - example: no "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")" will appear
; without "/k" or "/c" , cmd will open, but the "batchcode" lines will not be executed
/* ;notes....................................................... ;commands from the right and bellow /* will not be executed ..............................................................................................
& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.
&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
| [...] means that all commands will run at same time, but if one command fails to open, all command will fail too regardless their position (example):
notepad | mspaint | control
if notepad fails, all will fail too ; if control fails, all will fail too ; if mspaint fails, all will fail too
|| [...] command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
( ) [...] (command1 & command2)
Use to group or nest multiple commands.
; or , command1 parameter1;parameter2
Use to separate command parameters.
*/ ;notes....................................................... ;commands from the right and bellow */ will be executed ............................................................................................