Thread - Syntax & Usage | AutoHotkey

AutoHotkey

Thread

Sets the priority or interruptibility of threads. It can also temporarily disable all timers.

Thread, SubCommand , Value1, Value2

The SubCommand, Value1, and Value2 parameters are dependent upon each other and their usage is described below.

Sub-commands

For SubCommand, specify one of the following:

  • NoTimers: Prevents interruptions from any timers.
  • Priority: Changes the priority level of the current thread.
  • Interrupt: Changes the duration of interruptibility for newly launched threads.

NoTimers

Prevents interruptions from any timers.

Thread, NoTimers , TrueOrFalse

This sub-command prevents interruptions from any timers until the current thread either ends, executes Thread, NoTimers, false, or is interrupted by another thread that allows timers (in which case timers can interrupt the interrupting thread until it finishes).

If this sub-command is not used in the auto-execute section (top part of the script), all threads start off as interruptible by timers (though the settings of the Interrupt sub-command [below] will still apply). By contrast, if the auto-execute section turns on this sub-command but never turns it off, every newly launched thread (such as a hotkey, custom menu item, or timer) starts off immune to interruptions by timers.

Regardless of the default setting, timers will always operate when the script has no threads (unless Pause has been turned on).

Thread, NoTimers is equivalent to Thread, NoTimers, true. In addition, since TrueOrFalse is an expression, true resolves to 1, and false to 0.

Priority

Changes the priority level of the current thread.

Thread, Priority, Level

Specify for Level an integer between -2147483648 and 2147483647 (or an expression) to indicate the current thread's new priority. This has no effect on other threads. See Threads for details.

Due to its ability to buffer events, the command Critical is generally superior to this sub-command.

On a related note, the OS's priority level for the entire script can be changed via Process Priority. For example:

Process, Priority,, High

Interrupt

Changes the duration of interruptibility for newly launched threads.

Thread, Interrupt , Duration, LineCount

Note: This sub-command should be used sparingly because most scripts perform more consistently with settings close to the defaults.

By default, every newly launched thread is uninterruptible for a Duration of 15 milliseconds or a LineCount of 1000 script lines, whichever comes first. This gives the thread a chance to finish rather than being immediately interrupted by another thread that is waiting to launch (such as a buffered hotkey or a series of timed subroutines that are all due to be run).

Note: Any Duration less than 17 might result in a shorter actual duration or immediate interruption, since the system tick count has a minimum resolution of 10 to 16 milliseconds.

If either parameter is 0, each newly launched thread is immediately interruptible. If either parameter is -1, the thread cannot be interrupted as a result of that parameter. The maximum for both parameters is 2147483647.

This sub-command is global, meaning that all subsequent threads will obey it, even if the sub-command is used somewhere other than the auto-execute section. However, interrupted threads are unaffected because their period of uninterrutibility has already expired. Similarly, the current thread is unaffected except if it is uninterruptible at the time the LineCount parameter is changed, in which case the new LineCount will be in effect for it.

If a hotkey is pressed or a custom menu item is selected while the current thread is uninterruptible, that event will be buffered. In other words, it will launch when the current thread finishes or becomes interruptible, whichever comes first. The exception to this is when the current thread becomes interruptible before it finishes, and it is of higher priority than the buffered event; in this case the buffered event is unbuffered and discarded.

Regardless of this sub-command, a thread will become interruptible the moment it displays a MsgBox, InputBox, FileSelectFile, or FileSelectFolder dialog.

Either parameter can be left blank to avoid changing it.

If Critical is specified as the first line of the thread's subroutine or function, the thread starts out uninterruptible and the Interrupt sub-command has no effect. However, this does not apply to bound functions or user-defined function objects.

Remarks

Due to its greater flexibility and its ability to buffer events, the command Critical is generally more useful than the sub-commands Interrupt and Priority.

Related

Critical, Threads, Hotkey, Menu, SetTimer, Process

Examples

Thread, Priority, 1 ; Make priority of current thread slightly above average.
Thread, Interrupt, 0  ; Make each newly launched thread immediately interruptible.
Thread, Interrupt, 50, 2000  ; Make each thread interruptible after 50ms or 2000 lines, whichever comes first.