Autohotkey 腳本在互聯網重新連接時啟動程序

Autohotkey 腳本在互聯網重新連接時啟動程序

我正在嘗試編寫一個監視互聯網的腳本,如果它斷開連接則運行 chrome.exe重新連接

這是我到目前為止所擁有的;

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
        }
    }

問題是:

  • 當網路中斷時,顯示網路中斷的 MsgBox 不會立即出現,大約需要 6-7 秒
  • 當互聯網恢復時,確認重新連接的 Msgbox 和 Chrome.exe 不會啟動(並且互聯網肯定已經恢復,並且在 20000 毫秒內 - 我已經手動測試了這一點)

先致謝

答案1

您需要html := UrlDownloadToVar("http://www.google.com")在第二次檢查之前重新運行以更新該變數。

我認為運行一個 while 循環會更好。這樣,如果網路連線沒有恢復,它將繼續等待。透過這種方式,您可以檢查更短的時間間隔,並使腳本更迅速地回應。

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
}

如果您只想彈出一次訊息,可以將其放在if(!html) {}while 語句之前。

相關內容