PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/misc/Threads.htm

http://github.com/Lexikos/AutoHotkey_L-Docs
HTML | 32 lines | 30 code | 2 blank | 0 comment | 0 complexity | d53c8390424b2f21488f2aceb552c773 MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>Threads - Behaviour &amp; Priority | AutoHotkey</title>
  5. <meta name="description" content="The &quot;current thread&quot; is defined as the flow of execution invoked by the most recent event; examples include hotkeys, SetTimer subroutines, custom menu items, and GUI events." />
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8. <link href="../static/theme.css" rel="stylesheet" type="text/css" />
  9. <script src="../static/content.js" type="text/javascript"></script>
  10. </head>
  11. <body>
  12. <h1>Threads</h1>
  13. <p>The <em>current thread</em> is defined as the flow of execution invoked by the most recent event; examples include <a href="../Hotkeys.htm">hotkeys</a>, <a href="../commands/SetTimer.htm">SetTimer subroutines</a>, <a href="../commands/Menu.htm">custom menu items</a>, and <a href="../commands/Gui.htm#Events">GUI events</a>. The <em>current thread</em> can be executing commands within its own subroutine or within other subroutines called by that subroutine.</p>
  14. <p>Although AutoHotkey doesn't actually use multiple threads, it simulates some of that behavior: If a second thread is started -- such as by pressing another hotkey while the previous is still running -- the <em>current thread</em> will be interrupted (temporarily halted) to allow the new thread to become <em>current</em>. If a third thread is started while the second is still running, both the second and first will be in a dormant state, and so on.</p>
  15. <p>When the <em>current thread</em> finishes, the one most recently interrupted will be resumed, and so on, until all the threads finally finish. When resumed, a thread's settings for things such as <a href="ErrorLevel.htm">ErrorLevel</a> and <a href="../commands/SendMode.htm">SendMode</a> are automatically restored to what they were just prior to its interruption; in other words, a thread will experience no side-effects from having been interrupted (except for a possible change in the <a href="../commands/WinActivate.htm">active window</a>).</p>
  16. <p class="note"><strong>Note</strong>: The <a href="../commands/KeyHistory.htm">KeyHistory</a> command/menu-item shows how many threads are in an interrupted state and the <a href="../commands/ListHotkeys.htm">ListHotkeys</a> command/menu-item shows which hotkeys have threads.</p>
  17. <p>A single script can have multiple simultaneous <a href="../commands/MsgBox.htm">MsgBox</a>, <a href="../commands/InputBox.htm">InputBox</a>, <a href="../commands/FileSelectFile.htm">FileSelectFile</a>, and <a href="../commands/FileSelectFolder.htm">FileSelectFolder</a> dialogs. This is achieved by launching a new thread (via <a href="../Hotkeys.htm">hotkey</a>, <a href="../commands/SetTimer.htm">timed subroutine</a>, <a href="../commands/Menu.htm">custom menu item</a>, etc.) while a prior thread already has a dialog displayed.</p>
  18. <p>By default, a given <a href="../Hotkeys.htm">hotkey</a> or <a href="../Hotstrings.htm">hotstring</a> subroutine cannot be run a second time if it is already running. Use <a href="../commands/_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a> to change this behavior.</p>
  19. <h2>Thread Priority</h2>
  20. <p>Any thread (<a href="../Hotkeys.htm">hotkey</a>, <a href="../commands/SetTimer.htm">timed subroutine</a>, <a href="../commands/Menu.htm">custom menu item</a>, etc.) with a priority lower than that of the <em>current thread</em> cannot interrupt it. During that time, such timers will not run, and any attempt by the user to create a thread (such as by pressing a <a href="../Hotkeys.htm">hotkey</a> or <a href="../commands/GuiControls.htm#Button">GUI button</a>) will have no effect, nor will it be buffered. Because of this, it is usually best to design high priority threads to finish quickly, or use <a href="../commands/Critical.htm">Critical</a> instead of making them high priority.</p>
  21. <p>The default priority is 0. All threads use the default priority unless changed by one of the following methods:<br>
  22. 1) A timed subroutine is given a specific priority via <a href="../commands/SetTimer.htm">SetTimer</a>.<br>
  23. 2) A hotkey is given a specific priority via the <a href="../commands/Hotkey.htm">Hotkey</a> command.<br>
  24. 3)
  25. A <a href="../Hotstrings.htm">hotstring</a> is given a specific priority when it is defined, or via the <a href="../commands/_Hotstring.htm">#Hotstring</a> directive. <br>
  26. 4) A custom menu item is given a specific priority via the <a href="../commands/Menu.htm">Menu</a> command.<br>
  27. 5) The <em>current thread</em> sets its own priority via the <a href="../commands/Thread.htm">Thread</a> command.</p>
  28. <p>The <a href="../commands/OnExit.htm">OnExit</a> subroutine (if any) will always run when called for, regardless of the <em>current thread</em>'s priority.</p>
  29. </body>
  30. </html>