John117 Posted March 19, 2009 Share Posted March 19, 2009 (edited) Hey guys, I would like to run each case in the switch once before switching to a timer. In reality, the related code is very long and gets its run parameters from the switch. I need the function to run once with each switch being true. Then let the switch work with timers. If not possible, I will have to go from a switch and a few dozen lines, to separate functions and about 500 lines + Thanks! #include <Timers.au3> HotKeySet("{ESC}", "TerminateApp") $hWnd = GUICreate("Window", 500, 700, Default, Default) GUISetState(@SW_SHOW) ;New code to run each section of switch here. $hTimer1 = _Timer_SetTimer($hWnd, 3000, "somefunc", -1) $hTimer2 = _Timer_SetTimer($hWnd, 6000, "somefunc", -1) $hTimer3 = _Timer_SetTimer($hWnd, 9000, "somefunc", -1) While 1 Sleep(10) WEnd Func somefunc($hWnd, $iMsg, $iwParam, $ilParam) Switch $iwParam Case $hTimer1 MsgBox(0, "", "3Sec") ;Really is code that passes parameters to below function Case $hTimer2 MsgBox(0, "", "6Sec");Really is code that passes parameters to below function Case $hTimer3 MsgBox(0, "", "9Sec");Really is code that passes parameters to below function EndSwitch ;Below function ;Some related code here that gets variables from the switches and then runs function. EndFunc ;==>somefunc Func TerminateApp() _Timer_KillAllTimers($hWnd) GUIDelete() Exit 0 EndFunc ;==>TerminateApp Edited March 21, 2009 by Hatcheda Link to comment Share on other sites More sharing options...
John117 Posted March 19, 2009 Author Share Posted March 19, 2009 ok, here's what I came up with . . . anything better? expandcollapse popup#include <Timers.au3> HotKeySet("{ESC}", "TerminateApp") $hWnd = GUICreate("Window", 500, 700, Default, Default) GUISetState(@SW_SHOW) Dim $hWnd, $iMsg, $iwParam, $ilParam ;New code to run each section of switch here. $iwParam = 1001 Somefunc($hWnd, $iMsg, $iwParam, $ilParam) $iwParam = 1002 Somefunc($hWnd, $iMsg, $iwParam, $ilParam) $iwParam = 1003 Somefunc($hWnd, $iMsg, $iwParam, $ilParam) $hTimer1 = _Timer_SetTimer($hWnd, 3000, "somefunc", -1) $hTimer2 = _Timer_SetTimer($hWnd, 6000, "somefunc", -1) $hTimer3 = _Timer_SetTimer($hWnd, 9000, "somefunc", -1) While 1 Sleep(10) WEnd Func somefunc($hWnd, $iMsg, $iwParam, $ilParam) Switch $iwParam Case 1001 MsgBox(0, "", "3Sec") ;Really is code that passes parameters to below function Case 1002 MsgBox(0, "", "6Sec");Really is code that passes parameters to below function Case 1003 MsgBox(0, "", "9Sec");Really is code that passes parameters to below function EndSwitch ;Below function ;Some related code here that gets variables from the switches and then runs function. EndFunc ;==>somefunc Func TerminateApp() _Timer_KillAllTimers($hWnd) GUIDelete() Exit 0 EndFunc ;==>TerminateApp Link to comment Share on other sites More sharing options...
Spiff59 Posted March 19, 2009 Share Posted March 19, 2009 (edited) Each of your timers can trigger a separate finction, so you could toss the switch statement away... expandcollapse popup#include <Timers.au3> #include <GUIConstants.au3> HotKeySet("{ESC}", "TerminateApp") ;Dim $Timers = 3, $Delay[$Timers + 1] = [0, 3000, 6000, 9000] Dim $Timers = 4, $Delay[$Timers + 1] = [0, 1000, 4000, 10000, 15000] Dim $label[$Timers + 1], $hTimer[$Timers + 1], $hTimeroff $hWnd = GUICreate("Window", 90 + $Timers * 50, 120, Default, Default) GUICtrlSetDefColor(0xFF0000) For $x = 1 to $Timers $label[$x] = GUICtrlCreateLabel($Delay[$x] / 1000 & " sec", $x * 50, 40) $func = "SomeFunc" & $x Call($func,0,0,0,0) $hTimer[$x] = _Timer_SetTimer($hWnd, $Delay[$x], $func, -1) Next GUISetState(@SW_SHOW) ;------------------------------------------------------------------------------- While 1 Sleep(10) WEnd ;------------------------------------------------------------------------------- Func somefunc1($hWnd, $iMsg, $iwParam, $ilParam) GUICtrlSetState($label[1], $GUI_SHOW) $hTimeroff = _Timer_SetTimer($hWnd, 500, "off", -1) EndFunc Func somefunc2($hWnd, $iMsg, $iwParam, $ilParam) GUICtrlSetState($label[2], $GUI_SHOW) EndFunc Func somefunc3($hWnd, $iMsg, $iwParam, $ilParam) GUICtrlSetState($label[3], $GUI_SHOW) EndFunc Func somefunc4($hWnd, $iMsg, $iwParam, $ilParam) GUICtrlSetState($label[4], $GUI_SHOW) EndFunc Func off($hWnd, $iMsg, $iwParam, $ilParam) For $x = 1 to $Timers GUICtrlSetState($label[$x], $GUI_HIDE) Next _Timer_KillTimer($hWnd, $hTimeroff) EndFunc Func TerminateApp() _Timer_KillAllTimers($hWnd) GUIDelete() Exit 0 EndFunc;==>TerminateApp Pardon the extra junk in there... am bored so made it into a light-show edit:typo, and played with making the delays more flexible Edited March 20, 2009 by Spiff59 Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 19, 2009 Share Posted March 19, 2009 Hey guys, I would like to run each case in the switch once before switching to a timer. In reality, the related code is very long and gets its run parameters from the switch. I need the function to run once with each switch being true. Then let the switch work with timers. If not possible, I will have to go from a switch and a few dozen lines, to separate functions and about 500 lines + Thanks! #include <Timers.au3> HotKeySet("{ESC}", "TerminateApp") $hWnd = GUICreate("Window", 500, 700, Default, Default) GUISetState(@SW_SHOW) ;New code to run each section of switch here. $hTimer1 = _Timer_SetTimer($hWnd, 3000, "somefunc", -1) $hTimer2 = _Timer_SetTimer($hWnd, 6000, "somefunc", -1) $hTimer3 = _Timer_SetTimer($hWnd, 9000, "somefunc", -1) While 1 Sleep(10) WEnd Func somefunc($hWnd, $iMsg, $iwParam, $ilParam) Switch $iwParam Case $hTimer1 MsgBox(0, "", "3Sec");Really is code that passes parameters to below function Case $hTimer2 MsgBox(0, "", "6Sec");Really is code that passes parameters to below function Case $hTimer3 MsgBox(0, "", "9Sec");Really is code that passes parameters to below function EndSwitch ;Below function ;Some related code here that gets variables from the switches and then runs function. EndFunc ;==>somefunc Func TerminateApp() _Timer_KillAllTimers($hWnd) GUIDelete() Exit 0 EndFunc ;==>TerminateApp Here is an easy way: $hTimer1 = _Timer_SetTimer($hWnd, 3000, "somefunc", -1) somefunc("", "", $hTimer1, "") $hTimer2 = _Timer_SetTimer($hWnd, 6000, "somefunc", -1) somefunc("", "", $hTimer2, "") $hTimer3 = _Timer_SetTimer($hWnd, 9000, "somefunc", -1) somefunc("", "", $hTimer3, "") While 1 Sleep(10) WEnd Func somefunc($hWnd, $iMsg, $iwParam, $ilParam) Switch $iwParam Case $hTimer1 MsgBox(0, "", "3Sec");Really is code that passes parameters to below function Case $hTimer2 MsgBox(0, "", "6Sec");Really is code that passes parameters to below function Case $hTimer3 MsgBox(0, "", "9Sec");Really is code that passes parameters to below function EndSwitch If $hWnd = "" Then Return ;Below function ;Some related code here that gets variables from the switches and then runs function. EndFunc ;==>somefunc 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...
John117 Posted March 21, 2009 Author Share Posted March 21, 2009 Here is an easy way: $hTimer1 = _Timer_SetTimer($hWnd, 3000, "somefunc", -1) somefunc("", "", $hTimer1, "") $hTimer2 = _Timer_SetTimer($hWnd, 6000, "somefunc", -1) somefunc("", "", $hTimer2, "") $hTimer3 = _Timer_SetTimer($hWnd, 9000, "somefunc", -1) somefunc("", "", $hTimer3, "") While 1 Sleep(10) WEnd Func somefunc($hWnd, $iMsg, $iwParam, $ilParam) Switch $iwParam Case $hTimer1 MsgBox(0, "", "3Sec");Really is code that passes parameters to below function Case $hTimer2 MsgBox(0, "", "6Sec");Really is code that passes parameters to below function Case $hTimer3 MsgBox(0, "", "9Sec");Really is code that passes parameters to below function EndSwitch If $hWnd = "" Then Return ;Below function ;Some related code here that gets variables from the switches and then runs function. EndFunc;==>somefunc Good one! I will use that! This is what I had: For $P = 1 To 6 Step 1 $iwParam = 100 & $P GetPriceHistory($hWnd, $iMsg, $iwParam, $ilParam) ;ConsoleWrite($iwParam & @CRLF) Next _Timer_SetTimer($hWnd, 60 * 1000, "GetPriceHistory", -1) _Timer_SetTimer($hWnd, 60 * 5 * 1000, "GetPriceHistory", -1) _Timer_SetTimer($hWnd, 900 * 1000, "GetPriceHistory", -1) _Timer_SetTimer($hWnd, 1800 * 1000, "GetPriceHistory", -1) _Timer_SetTimer($hWnd, 3500 * 1000, "GetPriceHistory", -1) _Timer_SetTimer($hWnd, 86400 * 1000, "GetPriceHistory", -1) Each of your timers can trigger a separate finction, so you could toss the switch statement away... Thanks for the help but I cant use separate functions. The function is in reality about 70 lines after the switch. The only thing different with each run is a few variables. I added a switch in the front and back to load the variables and store the results for next run. Using separate functions would cause the code to be hundreds of lines. After I increase the function size this will result in thousands of lines saved. :-) Thanks though! Link to comment Share on other sites More sharing options...
Spiff59 Posted March 21, 2009 Share Posted March 21, 2009 Sounds like you could get rid of all the switch statements and end up with less code, rather than more. There's no reason when you know "which timer has been triggered" to throw that information away, and then add code to immediately retest to determine which timer had been triggered. Put the common portion of the code into it's own function and have the three timer functions call that. It's probably better structure to have the timer-specific code in their own paragraphs, and common processing in it's own paragraph. Anyway, there's a million ways to skin a cat, and I'm glad you have it working. Link to comment Share on other sites More sharing options...
John117 Posted March 22, 2009 Author Share Posted March 22, 2009 (edited) "There's no reason when you know "which timer has been triggered" to throw that information away, and then add code to immediately retest to determine which timer had been triggered."The point of the function is not to test which timer called it. That would be pointless. The functions purpose is to hit up an api for info, recalculate an indicator, then store the info for next period pass. It has to be stored as it calculates an RSI based on N periods. N periods is determined by the exact time called and much information passed to the function (period specific settings) by the switch. Each timer calculates a completely seperate Indicator and uses different displays."Sounds like you could get rid of all the switch statements and end up with less code, rather than more. . . It's probably better structure to have the timer-specific code in their own paragraphs, and common processing in it's own paragraph. . . Put the common portion of the code into it's own function and have the three timer functions call that. "There is not a common portion. They are all very different. -but can use the same code if variables are changed before running the function. I currently have six timers, not three. I will most likely keep 6 but will increase the function to 2X to 4X to include additional indicators ran at the same period.At current, I have about 98 lines for the function. If I were to seperate it and create 6 functions I would save 20 lines per function and bring my total count to 80 * 6I would then have to maintain 480 lines for updates. -then I add 2 or 3 new indicators and have 2000 lines instead of 300. Doesn't sound like a good deal to me. I wrote that first. This is smarter coding, sorry, but thanks for the help!Edit:The switch at the end turns common variable names used by all periods into period specific variables for storage. These are then avaliable only to that period on next run. That way I keep up with my averages for each timer. When the period runs again, I write them over the function before run. Edited March 22, 2009 by Hatcheda Link to comment Share on other sites More sharing options...
Spiff59 Posted March 22, 2009 Share Posted March 22, 2009 (edited) There is not a common portion. They are all very different. -but can use the same code if variables are changed before running the function. At current, I have about 98 lines for the function. If I were to seperate it and create 6 functions I would save 20 lines per function and bring my total count to 80 * 6. I would then have to maintain 480 lines for updates. The switch at the end turns common variable names used by all periods into period specific variables for storage. No biggy, is tough to make my point without seeing the 98 lines of code. But, "can use the same code if variables are changed" sounds like there's a common portion of code. As does "turns common variable names used by all periods into period specific variables". What I was thinking was instead of 100 lines of code, would be like 6 * 5 (timer-specific) plus 60 (common). The timer function calling the common function (minus switch statements) would be like: Func Timer1() set a couple variables process_common($mode) store a couple variables EndFunc Anyways, like I said, without seeing it, I can't see understand if I'm really nuts. I may be imaging something other than what you've got. Edit: It is no big deal. Hope it turns out real nice for you. Edited March 22, 2009 by Spiff59 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