AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Transform loop to timed sequence via SetTimer

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
TotAL_NewB



Joined: 13 Aug 2008
Posts: 9

PostPosted: Tue Aug 26, 2008 9:17 am    Post subject: Transform loop to timed sequence via SetTimer Reply with quote

This is what I have at the moment:

Code:
MyFunction()
{
   WinGet, myID, ID, MyWinTitle
   AnotherFunc(myID)
}

AnotherFunc(id)
{
   InitVar1 := ""
   InitVar2 := ""
   loop
   {
      ; do stuff here.
   }
}


MyFunction() gets trigged from an OnMessage event. Obviously the current code is very CPU intensive, so I'd like to run the loop in AnotherFunc(id) as a timed sequence.

Is this possible via SetTimer? The only problem I have with that is that it needs labels, and once i move all the code inside the loop to a label, the id parameter and all variables declared outside the loop won't be avaiable.

Please help Smile
Back to top
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1145
Location: The Interwebs

PostPosted: Tue Aug 26, 2008 9:22 am    Post subject: Re: Transform loop to timed sequence via SetTimer Reply with quote

TotAL_NewB wrote:
The only problem I have with that is that it needs labels, and once i move all the code inside the loop to a label, the id parameter and all variables declared outside the loop won't be avaiable


Declare the necessary variables as global so that the copies inside the function will be the same as those outside.
_________________
PlayAHK! Try it out Very Happy
Back to top
View user's profile Send private message AIM Address
TotAL_NewB



Joined: 13 Aug 2008
Posts: 9

PostPosted: Tue Aug 26, 2008 9:39 am    Post subject: Reply with quote

Thanks for the quick reponse.

The only problem I have with going global is that MyFunction() is called by the onmessage event and then gets the ID (say of the current active window). So the ID's might differ.

Or is is bad practice running the same timed sequence (with different parameters) within the same script?

So what I'm doing at the moment is running MyFunction() for 1st time from OnMessage. This continues to work with the current window's ID and the loop continues. Then another OnMessage event may be triggered on a different window. This code should then run and the loop will run on that window (while still also running on the first window).

What would be the best approach for this?
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1496

PostPosted: Tue Aug 26, 2008 10:36 am    Post subject: Reply with quote

I would hazard a guess that the best method to have a looping script for however many windows while maintaining performance and keeping CPU load low would be to have the script run however many instances of itself. (multiple instances is a multi-threading workaround)

So...
Code:
#SingleInstance, off
#Persistent
; #NoTrayIcon
OnMessage, 0x1234, MyFunction
OnExit, Cleanup ; do this on exit

; activate a termination hotkey for the first
; instance of the script only.
param1 = %1%
If (param1 != "spawned")
   Hotkey, esc, Cleanup, on
return

MyFunction(wParam, lParam, msg, hwnd)
{
   Global ; all variables in this function are global (except this function's input params)
   If (wParam = 0xDEAD) ; yes, this is a valid hex number, used as a 'kill code'
      Exitapp
   IfNotEqual, myID,, Return
   WinGet, myID, ID, MyWinTitle
   InitVar1 := ""
   InitVar2 := ""
   SetTimer, AnotherSub, 10
   Run %A_AhkPath% "%A_ScriptFullPath%" "spawned",,, SpawnPID
}

AnotherSub:
      ; do stuff here.
return

Cleanup:
; send the 'kill code' to another spawned instance
DetectHiddenWindows On
IfWinExist, Ahk_pID %SpawnPID%
   PostMessage, 0x1234, 0xDEAD, 0
; postmessage to the last found window from IfWinExist
Exitapp

untested. This should at least give you an idea about using multiple instances of a script to achieve multi-threading
_________________
My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags !
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group