run script as every 4 min ..?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Wazowski
Posts: 72
Joined: 16 Jun 2015, 21:04

run script as every 4 min ..?

12 Sep 2015, 09:24

Hello friends
As I make this script I always do the same thing every 4 min
Thanks in advance

Code: Select all

^t::
FileCopyDir,C:\Users\Fernando\Documents\My Games,%A_Desktop%\Terraria %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
return
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: run script as every 4 min ..?

12 Sep 2015, 10:37

Hi Wazowski,

Two ideas:

(1) Put the FileCopyDir command inside a 240-second Sleep loop (240,000 milliseconds). For example:

Code: Select all

Loop
{
  FileCopyDir,C:\Users\Fernando\Documents\My Games,%A_Desktop%\Terraria %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
  Sleep,240000
}
And put a shortcut to that script in your Startup program group or run it via the Task Scheduler "At log on" or "At startup".

(2) Put just the FileCopyDir command in a script and schedule it via the Task Scheduler to run every four minutes indefinitely (in Triggers>Advanced settings):

Image

And in the Settings tab, select the option not to start a new instance if the task is already running (in the event that it takes longer than four minutes to run). Regards, Joe
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: run script as every 4 min ..?

12 Sep 2015, 12:58

Probably simpler to use a timer for this, as the timer will run in a separate "thread" which will not interfere with other functions of the script.

eg:

Code: Select all

SetTimer, fc, 240000
; Other code can go here
return

fc:
   FileCopyDir,C:\Users\Fernando\Documents\My Games,%A_Desktop%\Terraria %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
   return

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: run script as every 4 min ..?

12 Sep 2015, 13:22

Hi evilC,
That's an interesting approach, and it raises a question. Is there a difference in efficiency between Sleep and SetTimer? I've often wondered exactly how Sleep is implemented in AHK. For example, does it take CPU cycles to sleep? If anyone can explain the internals of Sleep (and SetTimer), I'd appreciate it. Thanks, Joe
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: run script as every 4 min ..?

12 Sep 2015, 13:32

Sleep will pause the current "thread" while it sleeps. By default, ahk scripts have one "thread" for the main flow of execution, plus one "thread" for each hotkey.
This is why you could do code like this and the hotkey will still trigger:

Code: Select all

Loop {
   ; do something endlessly
   sleep 100
}

F12::
   soundbeep
   return
When a SetTimer triggers, the label is launched in a separate "thread". Therefore, anything you do in the timer thread (eg sleep) will not affect execution of the other "threads".

SetTimer can be hugely useful to implement "Asynchronous" behavior. You can use it to effectively kick off another thread. For example:

Code: Select all

; [normal flow of code]
SetTimer, SomethingThatCouldTakeAgesAndYouDontWantToWaitFor, -0   ; - means only do once, 0 means immediately
; Code instantly resumes here, while the "thread" goes off and does its stuff.
; [normal flow of code]

Note that I say "threads" not threads, as it is all in reality one thread, it just timeslices execution between the different "threads".
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: run script as every 4 min ..?

12 Sep 2015, 16:13

Great info, evilC - thanks for that. But I'm wondering exactly how AHK implements Sleep in Windows. Does it execute a NOP loop waiting for the time to pass (doubtful, of course)? Does it use Timers? I've been worried that it's not an efficient way to wait, but maybe it's a fine way to do it. Regards, Joe
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: run script as every 4 min ..?

12 Sep 2015, 19:10

Sorry, I don't know the answer to that question :(

Sleep / SetTimer is certainly less CPU intensive than some other methods (eg "QPC" DLL calls), but less accurate. For example, Sleep / SetTimer can vary by +/- 10ms. AFAIK it is not possible to sleep by less than 10ms.
This leads me to believe that it is about as efficient as you are gonna get without wasting a bunch of CPU cycles to do nothing, so I would say use either with impunity ;)
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: run script as every 4 min ..?

12 Sep 2015, 19:18

100 SetTimers @ 40ms, Each doing a 20ms sleep and displaying a tooltip.

Code: Select all

#Persistent
#SingleInstance force

mc := new MyClass()

class MyClass {
	__New(){
		Loop 100 {
			fn := this.DoSleep.Bind(this, A_Index)
			SetTimer, % fn, 40
		}
	}
	
	DoSleep(timernumber){
		tooltip % timernumber
		Sleep 20
	}
}
My CPU utilization does not move from 0%
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: run script as every 4 min ..?

12 Sep 2015, 20:24

Very nice! Your script registers zero CPU utilization here, too (W7/64-bit). Thanks!
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: run script as every 4 min ..?

12 Sep 2015, 20:34

Bear in mind that you would probably not get every timer firing @ exact 40ms intervals, as there is more than one timer scheduled for each 40ms "slot", the code was merely to illustrate you can use lots of timers and sleeps, and CPU utilization is not overly affected.

For example:

Code: Select all

#Persistent
#SingleInstance force
 
mc := new MyClass()
 
class MyClass {
	__New(){
		this.LastFire := []
		Loop 100 {
			this.LastFire[A_Index] := 0
			fn := this.DoSleep.Bind(this, A_Index)
			SetTimer, % fn, 40
		}
	}
 
	DoSleep(timernumber){
		tooltip % "Timer " timernumber " last fired " (A_TickCount - this.LastFire[timernumber]) / 1000 " seconds ago"
		this.LastFire[timernumber] := A_TickCount
		Sleep 20
	}
}
Each timer seems to fire every ~ 1.4 seconds on my system, and CPU utilization is still @ 0%, even with the new calculations.
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: run script as every 4 min ..?

12 Sep 2015, 20:43

Got it - thanks for the clarification.
Wazowski
Posts: 72
Joined: 16 Jun 2015, 21:04

Re: run script as every 4 min ..?

13 Sep 2015, 07:35

hey friends
share and one which I enhanced the credits I give them to friends YOUCEFHam the parallel forums autohotkey is http://goo.gl/aV4ISJ

Code: Select all

#Persistent
SetTimer, Check, 240000

Check:
SetTimer, Check, on
FileCopyDir,C:\Users\Fernando\Documents\My Games,%A_Desktop%\Terraria %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
return
far better for YOUCEFHam

Code: Select all

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}


F8::
toggle := !toggle
if toggle
{
   SetTimer, run_every_4_min, 240000
   gosub, run_every_4_min
   MsgBox, The script is running.
}
else
{
   SetTimer, run_every_4_min, Off
   MsgBox, The script is Stoped.
}
return

run_every_4_min:
FileCopyDir,C:\Users\Fernando\Documents\My Games,%A_Desktop%\Terraria %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
;use "A_MyDocuments" The full path and name of the current user's "My Documents" folder.
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, RandomBoy and 389 guests