Nahuel Posted October 25, 2007 Share Posted October 25, 2007 I made a script to keep my computer's clock updated. It checks the official time from a website and compares every 15 seconds. It works perfect, but I was wondering if what I was doing is.. 'efficient'. AdlibEnable("_Hora_actual",15000) Func _Hora_actual() Local $oHorax $oHTTP = ObjCreate("Microsoft.XMLHTTP") $oHTTP.Open("GET", "http://www.hidro.gov.ar/Hora/Hora.asp", False) $oHTTP.Send() $oHora = StringRegExp($oHTTP.ResponseText, "<h2 align='center'><font color='#0080C0'><font='Verdana'>(.*?)</font></h2>", 3) For $i = 0 To UBound($oHora) -1 $oHorax = $oHorax & $oHora[$i] Next If $oHora <> "" Then Return $oHorax Else SetError(1) Return -1 EndIf EndFunc The script calls _Hora_actual() every 15 seconds, which means that it creates a "Microsoft.XMLHTTP" object every time this function is called, which I thought wasn't good. Is this alright? Or can I do it in a different way? Link to comment Share on other sites More sharing options...
evilertoaster Posted October 25, 2007 Share Posted October 25, 2007 from the help file- When you assign an Object variable a different value, the 'pointer' will automatically be released. You can, for instance, force deletion of an Object by assigning it any number or any text value. $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") ; Object is created $oHTTP=0 ; Object is deleted You don't need to delete Objects when you are finished. If a script exits, AutoIt tries to release all active references to Objects that had been created in the script. Same thing happens when you had defined a local Object variable inside a function, and the function ends with a return. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 25, 2007 Moderators Share Posted October 25, 2007 (edited) I made a script to keep my computer's clock updated. It checks the official time from a website and compares every 15 seconds. It works perfect, but I was wondering if what I was doing is.. 'efficient'. AdlibEnable("_Hora_actual",15000) Func _Hora_actual() Local $oHorax $oHTTP = ObjCreate("Microsoft.XMLHTTP") $oHTTP.Open("GET", "http://www.hidro.gov.ar/Hora/Hora.asp", False) $oHTTP.Send() $oHora = StringRegExp($oHTTP.ResponseText, "<h2 align='center'><font color='#0080C0'><font='Verdana'>(.*?)</font></h2>", 3) For $i = 0 To UBound($oHora) -1 $oHorax = $oHorax & $oHora[$i] Next If $oHora <> "" Then Return $oHorax Else SetError(1) Return -1 EndIf EndFunc The script calls _Hora_actual() every 15 seconds, which means that it creates a "Microsoft.XMLHTTP" object every time this function is called, which I thought wasn't good. Is this alright? Or can I do it in a different way?Could shorten that function a bit (Changed a bit of the RegExp as well):Func _Hora_actual() Local $oHTTP, $oHora $oHTTP = ObjCreate("Microsoft.XMLHTTP") $oHTTP.Open("GET", "http://www.hidro.gov.ar/Hora/Hora.asp", False) $oHTTP.Send() $oHora = StringRegExp($oHTTP.ResponseText, "\<h2 align='center'>\<font color='#0080C0'>\<font='Verdana'>.*?(\d+:\d+:\d+).*?\<\/font>\<\/h2>", 3) If IsArray($oHora) = 0 Then SetError(1, 0, -1) Return $oHora[0] EndFunc How are you updating the clock? Edit: Forgot to escape some chars. Edited October 25, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Nahuel Posted October 25, 2007 Author Share Posted October 25, 2007 (edited) Thank you I did read that before. It says I don't need to delete it if it is defined as local Object variable inside a function. If I add this line at the beginning:Global $oHTTP = ObjCreate("Microsoft.XMLHTTP")and remove it from inside the function, will I be working with the same object every time?-edit-Thanks Smoke! Regular expressions are confusing. I use _SetTime() to update the clock. Edited October 25, 2007 by Nahuel Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 25, 2007 Moderators Share Posted October 25, 2007 (edited) Thank you I did read that before. It says I don't need to delete it if it is defined as local Object variable inside a function. If I add this line at the beginning: Global $oHTTP = ObjCreate("Microsoft.XMLHTTP") and remove it from inside the function, will I be working with the same object every time? -edit- Thanks Smoke! Regular expressions are confusing. I use _SetTime() to update the clock.Global $oHTTP $oHTTP = ObjCreate("Microsoft.XMLHTTP") AdlibEnable("_Hora_actual",5000) While 1 Sleep(1000) WEnd Func _Hora_actual() Local $oHora If IsObj($oHTTP) = 0 Then $oHTTP = ObjCreate("Microsoft.XMLHTTP") $oHTTP.Open("GET", "http://www.hidro.gov.ar/Hora/Hora.asp", False) $oHTTP.Send() $oHora = StringRegExp($oHTTP.ResponseText, "\<h2 align='center'>\<font color='#0080C0'>\<font='Verdana'>.*?(\d+:\d+:\d+).*?\<\/font>\<\/h2>", 3) If IsArray($oHora) = 0 Then SetError(1, 0, -1) ToolTip($oHora[0], 0, 0) Return $oHora[0] EndFuncCould always test and see ... (add a bit of error checking to yours I hope though). EDIT: I'd like to add this though... I don't think this is a very safe way to set the time on your PC. If the server is slow, or it takes time to parse the file... your PC is going to slowly leak milliseconds/seconds. Edited October 25, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Nahuel Posted October 25, 2007 Author Share Posted October 25, 2007 Yup, I tried and it works Just wanted to make sure. Thank you! The only bad thing is that it works only for GMT-3 time zones Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 25, 2007 Moderators Share Posted October 25, 2007 Yup, I tried and it works Just wanted to make sure. Thank you!The only bad thing is that it works only for GMT-3 time zones Use this site then: http://www.timeanddate.com/worldclockI already have it parsed for you here:http://www.autoitscript.com/forum/index.ph...st&p=422903 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Nahuel Posted October 25, 2007 Author Share Posted October 25, 2007 Use this site then: http://www.timeanddate.com/worldclockI already have it parsed for you here:http://www.autoitscript.com/forum/index.ph...st&p=422903 That can't be more perfect. Thank you... Link to comment Share on other sites More sharing options...
PsaltyDS Posted October 25, 2007 Share Posted October 25, 2007 Looks like you got better stuff from SmOke_N while I was trying it out. But just for giggles, this is how I modded it: 1. Replaced the call to XMLHTTP with InetGet(). 2. Added a way to kill it. 3. Added a While loop to keep the script alive. 4. Added a way to see the results. 5. Added a timeout so it wouldn't hang if the site was not available. expandcollapse popupGlobal $sFile = "C:\Temp\Hora.asp" HotKeySet("^q", "_Quit") ; Ctrl-q to quit AdlibEnable("_Hora_actual", 15000) While 1 Sleep(100) WEnd Func _Hora_actual() Local $sHorax = "" InetGet("http://www.hidro.gov.ar/Hora/Hora.asp", $sFile, 1, 1) Local $iTime = TimerInit() Do If TimerDiff($iTime) > 10000 Then InetGet("abort") TrayTip("_Hora_actual()", "@error = 3", 5) Return SetError(3, 0, -1) EndIf Until @InetGetActive = 0 Local $avHora = StringRegExp(FileRead($sFile), "<h2 align='center'><font color='#0080C0'><font='Verdana'>(.*?)</font></h2>", 3) Local $iSavErr = @error If $iSavErr = 0 Then For $i = 0 To UBound($avHora) - 1 $sHorax &= $avHora[$i] Next TrayTip("_Hora_actual()", "$sHorax = " & $sHorax, 5) Return $sHorax Else TrayTip("_Hora_actual()", "@error = " & $iSavErr, 5) Return SetError($iSavErr, 0, -1) EndIf EndFunc ;==>_Hora_actual Func _Quit() FileDelete($sFile) Exit EndFunc Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Nahuel Posted October 25, 2007 Author Share Posted October 25, 2007 That's awesome Psalty! But yeah Smoke gave me something to get the time from lots of cities Link to comment Share on other sites More sharing options...
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