Sign in to follow this  
Followers 0
Sinaptics

Timer Help

21 posts in this topic

I'm working in an application that saves every 20 minutes, 10 seconds. During the save, the application doesn't accept input. The save lasts for ~10 seconds.

I need to create a timer loop that will pause my code until the save is complete, but i'm having trouble and was hoping someone could help.

Since the script doesn't run constantly, I'm going to have to start it while it's saving to get the timing right. So basically, while the time is running, I want it to pause the script for the first 30 seconds (to cover while it's saving), execute my code, then pause again 2 minutes prior to the save, restart the timer at the save (which will again pause for the first 30 seconds) then run my code again, etc.

This is probably extremely simple, but I've been fighting with it for two days and just can't seem to get it right.

Share this post


Link to post
Share on other sites



Sleep may be your command.

or AdlibRegister

Share this post


Link to post
Share on other sites

Sleep may be your command.

or AdlibRegister

Sleep won't work as it has to pause at specific time intervals independant of code progress. While I can specify when Adlibregister kicks in, it'll only work if an error is returned. A timer would still be better for this situation.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

sinaptics,

An adlib would work perfectly. Get the file modified time when you start the save/script. Start an adlib the compares the previously gotten file modified time to the current file modified time every xx seconds. When they are different set a flag and kill the adlib.

kylomas

edit: if it is a large file you can also play around with trying to get exclusive use of the file. See the _winapiEX* functions.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

If you're interested in timers check this function:

_Timer_SetTimer

It's easier if you need multiple timers,

but if you need only one, then the best solution is:

AdlibRegister

Share this post


Link to post
Share on other sites

You can use multiple Adlib timers in the same script.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

sinaptics,

This routine can detect when I do a save to a file open in WORD. Maybe you can adapt it. Not sure whether the handle info is updated at the start of the save or at the end. I don't have anything handy that takes more than 1 sec to save.

#Include <APIConstants.au3>
#Include <Date.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
local $tFILETIME, $hFile, $aInfo
adlibregister('get_file_info',3000)
while 1
 sleep(999999)
wend
func get_file_info()
 $hFile = _WinAPI_CreateFile(@Scriptdir & '\test.doc', 2, 0, 6)
 $aInfo = _WinAPI_GetFileInformationByHandle($hFile)
 If IsArray($aInfo) Then
  For $i = 1 To 3
   If IsDllStruct($aInfo[$i]) Then
    $tFILETIME = _Date_Time_FileTimeToLocalFileTime(DllStructGetPtr($aInfo[$i]))
    $aInfo[$i] = _Date_Time_FileTimeToSystemTime(DllStructGetPtr($tFILETIME))
    $aInfo[$i] = _Date_Time_SystemTimeToDateTimeStr($aInfo[$i])
   Else
    $aInfo[$i] = 'Unknown'
   EndIf
  Next
  ConsoleWrite('Path:         ' & _WinAPI_GetFinalPathNameByHandle($hFile) & @CR)
  ConsoleWrite('Attributes:    0x' & Hex($aInfo[0]) & @CR)
  ConsoleWrite('Created:       ' & $aInfo[1] & @CR)
  ConsoleWrite('Accessed:     ' & $aInfo[2] & @CR)
  ConsoleWrite('Modified:     ' & $aInfo[3] & @CR)
  ConsoleWrite('Volume serial: ' & $aInfo[4] & @CR)
  ConsoleWrite('Size:         ' & $aInfo[5] & @CR)
  ConsoleWrite('Links:       ' & $aInfo[6] & @CR)
  ConsoleWrite('ID:         ' & $aInfo[7] & @CR)
 EndIf
 _WinAPI_CloseHandle($hFile)
endfunc

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

My definition of a timer is a function that is run every interval.

That is what AdLibRegister provides:

Local $ticks = TimerInit()
AdlibRegister("_Timer",100)

while True
ToolTip(TimerDiff($ticks))
WEnd

Func _Timer()
if TimerDiff($ticks) > 3000 then
MsgBox(0,"Alarm","Beep!")
Exit
endif
EndFunc

Wasn't thinking of using it like that, however, I could see using it like this, if it'll work. How do I get the timerinit to reset at a specific time?

Local $ticks = TimerInit()
AdlibRegister("_Timer",100)
while True ToolTip(TimerDiff($ticks))
WEnd

Func _Timer()
if TimerDiff($ticks) < 1000 then
sleep (1000)
elseif TimerDiff($ticks) > 15000
sleep (2000)
Exit
endif
EndFunc

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

sinaptics,

1 - adlibs are blocking so you do not want to use sleeps in them, particularly when the sleeps may exceed the adlib iteration interval.

If you call the adlib before the previous instance ends then the call time is reset. The net effect is that your adlib routine just

keeps running (defeating the purpose of using an adlib)

This is incorrect, in fact I believe that your script would lock up because the adlib's would stack up.

2 - a timer is just a counter. you reset it by issuing another call to timerinit with the same variable. You can use multiple timerinit's if required.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

sinaptics,

1 - adlibs are blocking so you do not want to use sleeps in them, particularly when the sleeps may exceed the adlib iteration interval.

If you call the adlib before the previous instance ends then the call time is reset. The net effect is that your adlib routine just

keeps running (defeating the purpose of using an adlib)

This is incorrect, in fact I believe that your script would lock up because the adlib's would stack up.

2 - a timer is just a counter. you reset it by issuing another call to timerinit with the same variable. You can use multiple timerinit's if required.

kylomas

Like this?

$timer = TimerInit()
$Pause = False

While $timer > 0
then

If $timer < 1000
then _Pause
Elseif $timer > 1000 and < 10000
then _Code ;application code
Elseif $timer > 10000 and < 15000
then _Pause
Else
$timer = TimerInit()
Endif

Func _Pause()
$Pause = Not $Pause
If $Pause Then
TrayTip("", "I am paused", 3)
Else
TrayTip("", "I am running", 3)
Endif

EndFunc

Share this post


Link to post
Share on other sites

sinaptics,

This is getting wide of your OP. What is the application you are trying to interface with?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

It's a legacy accounting system within a networked environment. I'm automating coding through a front-end. I'm fairly new at this, so please bear with me, still trying to figure out proper syntax.

I'm trying to take things piecemeal. One of my first concerns is that the system saves every 20 minutes. If my coding script is running, the save will send it off the rails because my script will continue functioning while the save happens, then it'll be in the wrong spot when it resumes. The timer is going to be my top level loop that will pause all the other code.

My idea is to carve out 2 minutes prior to the 20 minute mark, reset the timer at the 20 minute mark, and continue the pause into the first 30 seconds of the new timer. I can make things prettier later, I'm just going for functional at this point.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

If you created a script that did the following - would it satisfy your needs

1. get the time (#a)

2. do actions until time is 18 minutes past #a

3. wait until time is 20 minutes past #a

4. repeat 1

Note: if the system gives some user visible indication of the saving action - you could detect that and restart your actions from the known point.

Edited by MouseSpotter

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

If you created a script that did the following - would it satisfy your needs

1. get the time (#a)

2. do actions until time is 18 minutes past #a

3. wait until time is 20 minutes past #a

4. repeat 1

Note: if the system gives some user visible indication of the saving action - you could detect that and restart your actions from the known point.

I had thought of that.

It would, however, it's actually 20 minutes, 10 seconds. That complicates just going off the time unless some type of multiple is used. I thought it would be safer to to just give it a little lead time, in case I don't get the start exactly right. That way, as the hours pass, and the time creeps forward, I'll still be in good shape.

There is non-selectable text that displays that it's saving and it goes unresponsive for about 10 seconds. I don't have access to the background accounting files, which is probably smart ;)

Edited by Sinaptics

Share this post


Link to post
Share on other sites

Sinaptics,

What does this mean?

automating coding

Data entry?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Durring the 10 seconds of saving, what is the state of the window? It is probably NOT Enabled...so you should check states are as expected prior to doing actions

or, maybe you can add an adlib check on state of the window...then you don't have to worry about the timing.

While Not BitAND(WinGetState($yourWindowHandle), 4)
  Sleep (100)
Wend

Also, with better scripting practices, you would not have the issue with the script proceeding, when prior steps fails.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Sinaptics,

This question is too obvious but I'll ask it anyway, do you have any control over the saving process?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Sinaptics,

What does this mean?

Data entry?

kylomas

Data entry.

Share this post


Link to post
Share on other sites

Durring the 10 seconds of saving, what is the state of the window? It is probably NOT Enabled...so you should check states are as expected prior to doing actions

or, maybe you can add an adlib check on state of the window...then you don't have to worry about the timing.

While Not BitAND(WinGetState($yourWindowHandle), 4)
Sleep (100)
Wend

Also, with better scripting practices, you would not have the issue with the script proceeding, when prior steps fails.

I'll check this. It's possible that the window becomes inactive. I had assumed that it just locked editing internally. However, just to become more adept at this, I'd still like to figure out how to make a timer work as I envisioned it.

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