Script de Autohotkey para iniciar el programa al volver a conectarse a Internet

Script de Autohotkey para iniciar el programa al volver a conectarse a Internet

Estoy intentando codificar un script que monitorea Internet y, si se desconecta, ejecutar chrome.exe enreconexión.

Esto es lo que tengo hasta ahora;

UrlDownloadToVar(URL) {
ComObjError(false)
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", URL)
WebRequest.Send()
Return WebRequest.ResponseText
}

#Persistent
SetTimer, CheckInternet, 100
Return

CheckInternet:
html := UrlDownloadToVar("http://www.google.com")
if html
    {}
else
    {
    MsgBox,, Internet status, not working will check again later, 1
    sleep, 20000
    if html
        {
        MsgBox,, Internet status, 2nd  check = working, 5
        Run chrome.exe
        }
    }

Los problemas son:

  • El cuadro de mensajes que muestra la desconexión de Internet no aparece inmediatamente cuando Internet se desconecta, tarda entre 6 y 7 segundos.
  • Msgbox que confirma la reconexión y Chrome.exe no se inician cuando vuelve Internet (y Internet definitivamente ha regresado, y dentro de 20000 milisegundos; lo he probado manualmente)

gracias de antemano

Respuesta1

Debe volver a ejecutar html := UrlDownloadToVar("http://www.google.com")antes de la segunda verificación para actualizar esa variable.

Creo que sería mejor ejecutar un bucle while. De esta forma, si la conexión a Internet no vuelve, seguirá esperando. De esta manera puede comprobar en intervalos más cortos y hacer que el script responda más rápidamente.

html := UrlDownloadToVar("http://www.google.com")
while(!html) {
    MsgBox,, Internet status, not working will check again later, 1
    sleep, 20000
    html := UrlDownloadToVar("http://www.google.com")
}
MsgBox,, Internet status, 2nd  check = working, 5
    Run chrome.exe
}

Si solo desea que el mensaje aparezca una vez, puede incluirlo en una if(!html) {}declaración anterior al while.

información relacionada