Advertisement
Stewie410

redditCommentSource_2021-04-24

Apr 25th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. Inline code is done by surrounding the code with _**single**_ backticks, eg:
  2.  
  3. `example`
  4.  
  5. Code blocks are handled by prepending _each_ line of the code with _**four spaces**_, eg:
  6.  
  7. <space><space><space><space>example
  8.  
  9. As for the code -- I'd work on getting your script to work _without an infinite loop_ first. Get the buttons, labels & progress controls working like you'd want...then tackle the looping portion of it. While I've not personally used `SetTimer` for my projects, here it would be the right call imo; as /u/joesii pointed out in his second comment.
  10.  
  11. Here's an example of why we're leaning that way:
  12.  
  13. ; Global variables
  14. val := 0
  15. inc := 1
  16.  
  17. ; Update/Game Loop
  18. SetTimer, myLoop, 200
  19.  
  20. ; Build GUI
  21. Gui, Add, Text, w200 +HWNDhText, %val%
  22. Gui, Add, Button, +ginvert, Invert
  23. Gui, Show, % "x" . (A_ScreenWidth / 2) . " y" . (A_ScreenHeight / 2), Example
  24.  
  25. ; End the auto-execute section
  26. return
  27.  
  28. ; Exit application if GUI is closed (or crashes)
  29. GuiEscape(GuiHwnd) {
  30. ExitApp
  31. }
  32.  
  33. GuiClose(GuiHwnd) {
  34. ExitApp
  35. }
  36.  
  37. ; Update/Game Logic
  38. myLoop() {
  39. ; Import global variables
  40. global val
  41. global hText
  42. global inc
  43.  
  44. ; Increment Text value
  45. val += inc
  46.  
  47. ; Update Text
  48. GuiControll,, %hText%, %val%
  49. }
  50.  
  51. ; Action on Button Click
  52. invert() {
  53. ; Import global variables
  54. global inc
  55.  
  56. ; Invert Increment Value
  57. inc *= -1
  58. }
  59.  
  60. There's a couple of extra functions in there to handle exiting the application when you close to GUI, as otherwise it'll just keep running in the background...
  61.  
  62. But with the above example, the `Text` control continues to update every 200ms. When the `Button` control is clicked; the `invert()` function is called, which inverts the increment value...but, _the timer keeps running_.
  63.  
  64. With the infinite loop example, while you _could_ get that working, you may find that the button's gSub (or function) is never called; as the `Loop {}` statement is still executing...holding up the thread.
  65.  
  66. Also, small correction -- AHK does have some kind of multithreading, but does not give us nearly the same control over it as traditional programming languages. In this case, the `timer` is, effectively, running on another thread ([source](https://www.autohotkey.com/docs/misc/Threads.htm)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement