Sign in to follow this  
Followers 0
rezz

IE Refresh

5 posts in this topic

I have a webpage that times out after about 4 minutes and need to access it at unknown intervals. The object then is to get the lastmodified time of the page and refresh if that time is over 3 minutes before the current time. Refreshing on a timer is not desired because that may interfere if data is being entered on the page and a refresh occurs.

So far I have came up with this which works but is a mess:

#include <IE.au3>
#include <Timers.au3>
#include <Date.au3>

$oIE = _IEAttach("", "INSTANCE", 1)
$oDocument = _IEDocGetObj($oIE)
Local $Time = $odocument.lastModified
Local $Time1 = StringTrimLeft($Time, 14)
$Time3 = StringTrimRight($Time1, 3)

While 1
    Sleep(20000)
    $Time2 = _NowTime(5)
    $Time5 = StringTrimLeft($Time2, 3)
    $Time6 = StringTrimRight($Time5, 3)

    ConsoleWrite($Time & @CRLF) ;For testing only
    ConsoleWrite($Time1 & @CRLF);For testing only
    ConsoleWrite($Time3 & @CRLF);For testing only
    ConsoleWrite($Time5 & @CRLF);For testing only
    ConsoleWrite($Time6 & @CRLF);For testing only

    $TimeDiff = $Time6 - $Time3

    If $TimeDiff >= 3 Then
        MsgBox(0, "TimeDiff: ", $TimeDiff)
        _IEAction($oIE, "refresh")
    EndIf
WEnd

Here's the console printout while running:

01/07/2011 10:01:43 ; $odocument.lastModified

01:43 ;StringTrimLeft to remove date and hour

01 ;StringTrimRight to remove seconds

10:02:03 ;$Time2 = _NowTime(5)

02:03 ;StringTrimLeft to remove hour

02 ;StringTrimRight to remove seconds

01/07/2011 10:01:43 ;Starting over

01:43

01

10:02:23

02:23

02

01/07/2011 10:01:43

01:43

01

10:02:44

02:44

02

01/07/2011 10:01:43

01:43

01

10:03:04

03:04

03

01/07/2011 10:01:43

01:43

01

10:03:24

03:24

03

01/07/2011 10:01:43

01:43

01

10:03:44

03:44

03

01/07/2011 10:01:43

01:43

01

10:04:04

04:04

04 ; >>>Refresh since timediff is >= 3

+>10:04:09 AutoIT3.exe ended.rc:0

>Exit code: 0 Time: 147.234

This works to refresh the page when the $TimeDiff >= 3 minutes

Questions:

1. What's a better way to do this?

2. How can I keep this while loop from interferring with the main gui?

If it is running inside the gui and a button is clicked to call another function would this cause a crash?

It appeared that it did cause a crash and the main gui closed. This loop needs to run in the background while other tasks are being done with

the main script.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

1. Two methods are better at doing this:

Method one: Use a long sleep that exactly fits the time you want it to wait. 3 minutes = 180000 milliseconds, so simply Sleep(180000) and then refresh.

Method two: TimerInit() and TimerDiff(), considering what you already have you will understand these functions by looking at the help file examples for them. You use TimerDiff to get the time passed in milliseconds since you called TimerInit(). You can use really short sleeps using this method like 10 milliseconds and up.

2. Two methods (again) will solve your problem

Method one: Use the GUIOnEventMode (see AutoItSetOption and GUISetOnEvent and GUICtrlSetOnEvent in the help file). They break out of a long sleep: so this works with previously mentioned method one.

Method two: Use very short sleeps with TimerInit() and TimerDiff() and poll the GUI normally with GUIGetMsg(), and right after that TimerDiff. This doesn't fit with method one: Your crash was caused because GUIGetMsg wasn't called often enough and that made Windows think your window was unresponsive (while your code was running).

Edited by Manadar

Share this post


Link to post
Share on other sites

Thank you for that Manadar

I tried the guioneventmode and increased the time to check the page lastmodified time.

But....my main gui stops working. So I'm still trying to get that figured out.

What I have is a gui which has buttons and fields. Upon clicking a button it retrieves info from a webpage and populates the gui fields.

Another button is then clicked and the gui info is sent to notepad to as a log entry.

Share this post


Link to post
Share on other sites

Sleep(20000) in your loop is too high. The GUI is not being polled fast enough.

Something like this is more like what Manadar suggested.

; With GUI
Local $timer = TimerInit(); start timer

While 1
    If TimerDiff($timer) > 180000 Then; 3 minutes


        ; Code goes here


        $timer = TimerInit(); restart timer at bottom of code
    EndIf

    $nMsg = GUIGetMsg()
    If $nMsg = -3 Then ExitLoop
WEnd

; ===============================================================

; With no GUI
Local $timer = TimerInit(); start timer

While 1
    If TimerDiff($timer) > 180000 Then; 3 minutes


        ; Code goes here


        $timer = TimerInit(); restart timer at bottom of code
    EndIf

    Sleep(10)
WEnd

Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

ripdad came up with a great way to do this:

Here it it is:

Global $LastRefresh = Number(@Mon & @Mday & @Year & @Hour & @Min & @Sec)
$LastRefresh -= 10000; set back a bit for first start

Local $Button1 = GUICtrlCreateButton("Refresh On", 330, 320, 60, 20) 
Local $Button2 = GUICtrlCreateButton("Refresh Off", 330, 350, 60, 20)

Func PageRefresh()
    Local $oIE = _IEAttach("", "INSTANCE", 1)
    Local $oDocument = _IEDocGetObj($oIE)
    If Not IsObj($oDocument) Then
        ConsoleWrite('Error - Not an Object' & @CRLF & 'IE Not Running?' & @CRLF); for testing only
        Return
    EndIf
    Local $Time = $odocument.lastModified
    $Time = StringRegExpReplace($Time, '(/* *:*)', '')
    Local $TimeDiff = $Time - $LastRefresh
    ConsoleWrite($LastRefresh & @CRLF & $Time & @CRLF & $TimeDiff & @CRLF); for testing only
    If $TimeDiff < 300 Then Return; 3 Minutes
    _IEAction($oIE, "REFRESH")
    If @error Then
        ConsoleWrite("Error: " & @error & @CRLF); for testing only
        Return
    Else
        ConsoleWrite("Refresh - TimeDiff: " & $TimeDiff & @CRLF); for testing only
        $LastRefresh = $Time
    EndIf
EndFunc

This checks the webpage for last refreshed time and then refreshes it if it has been over 3 mins since last refresh.

Thanks to ripdad for this. It works well and doesn't refresh unless the page actually needs it.

Edited by rezz

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0