MattX Posted June 13, 2005 Posted June 13, 2005 I'm a bit stuck - I want this little prog to keep looping until I break out of it - I wanted to use a 'continueloop' but cannot work out if this is the best way or shall I use something else instead - some help would be appreicated. The prog just keeps pinging an address, if it fails then it logs it, if not then I want it to keep going.....and going.....and going.... expandcollapse popup$var = Ping("10.0.0.4") Dim $sLogPath = "c:\matt\log.txt" Dim $sLogMsg = "Failed Ping at " If $var Then Else _FileWriteLog($sLogPath, $sLogMsg) Sleep(60000) EndIf Func _FileWriteLog($sLogPath, $sLogMsg) Local $sDateNow Local $sTimeNow Local $sMsg Local $hOpenFile Local $hWriteFile $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg $hOpenFile = FileOpen($sLogPath, 1) If $hOpenFile = -1 Then SetError(1) Return 0 EndIf $hWriteFile = FileWriteLine($hOpenFile, $sMsg) If $hWriteFile = -1 Then SetError(2) Return 0 EndIf FileClose($hOpenFile) Return 1 EndFunc
buzz44 Posted June 13, 2005 Posted June 13, 2005 (edited) While Loop?expandcollapse popupDim $sLogPath = "c:\matt\log.txt" Dim $sLogMsg = "Failed Ping at " While 1 $var = Ping("10.0.0.4") If $var = 0 Then _FileWriteLog($sLogPath, $sLogMsg) Sleep(60000) EndIf Wend Func _FileWriteLog($sLogPath, $sLogMsg) Local $sDateNow Local $sTimeNow Local $sMsg Local $hOpenFile Local $hWriteFile $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg $hOpenFile = FileOpen($sLogPath, 1) If $hOpenFile = -1 Then SetError(1) Return 0 EndIf $hWriteFile = FileWriteLine($hOpenFile, $sMsg) If $hWriteFile = -1 Then SetError(2) Return 0 EndIf FileClose($hOpenFile) Return 1 EndFuncI also fixed you If $var statement.Edit: You might also want to include the nature of the error using @error to determine the failure.When the function fails (returns 0) @error contains extended information: 1 = Host is offline 2 = Host is unreachable 3 = Bad destination 4 = Other errors Edited June 13, 2005 by Burrup qq
MattX Posted June 13, 2005 Author Posted June 13, 2005 Christ that was quick !! Many thanks.While Loop?expandcollapse popupDim $sLogPath = "c:\matt\log.txt" Dim $sLogMsg = "Failed Ping at " While 1 $var = Ping("10.0.0.4") If $var = 0 Then _FileWriteLog($sLogPath, $sLogMsg) Sleep(60000) EndIf Wend Func _FileWriteLog($sLogPath, $sLogMsg) Local $sDateNow Local $sTimeNow Local $sMsg Local $hOpenFile Local $hWriteFile $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg $hOpenFile = FileOpen($sLogPath, 1) If $hOpenFile = -1 Then SetError(1) Return 0 EndIf $hWriteFile = FileWriteLine($hOpenFile, $sMsg) If $hWriteFile = -1 Then SetError(2) Return 0 EndIf FileClose($hOpenFile) Return 1 EndFuncI also fixed you If $var statement.<{POST_SNAPBACK}>
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now