Critical

Prevents the current thread from being interrupted by other threads, or enables it to be interrupted.

Critical , OnOffNumeric

Parameters

OnOffNumeric

If blank or omitted, it defaults to On. Otherwise, specify one of the following:

On: The current thread is made critical, meaning that it cannot be interrupted by another thread.

Off: The current thread immediately becomes interruptible, regardless of the settings of Thread Interrupt. See Critical Off for details.

(Numeric) [v1.0.47+]: Specify a positive number to turn on Critical but also change the number of milliseconds between checks of the internal message queue. See Message Check Interval for details. [v1.0.48+]: Specifying 0 turns off Critical.

Behavior of Critical Threads

Critical threads are uninterruptible; for details, see Threads.

A critical thread becomes interruptible when a message box or other dialog is displayed. However, unlike Thread Interrupt, the thread becomes critical again after the user dismisses the dialog.

Critical Off

When buffered events are waiting to start new threads, using Critical Off will not result in an immediate interruption of the current thread. Instead, an average of 5 milliseconds will pass before an interruption occurs. This makes it more than 99.999% likely that at least one line after Critical Off will execute before an interruption. You can force interruptions to occur immediately by means of a delay such as a Sleep -1 or a WinWait for a window that does not yet exist.

Critical Off cancels the current thread's period of uninterruptibility even if the thread was not Critical, thereby letting events such as GuiSize be processed sooner or more predictably.

Thread Settings

See A_IsCritical for how to save and restore the current setting of Critical. However, since Critical is a thread-specific setting, when a critical thread ends, the underlying/resumed thread (if any) will be automatically noncritical. Consequently there is no need to do Critical Off right before ending a thread.

If Critical is not used in the auto-execute section (top part of the script), all threads start off as noncritical (though the settings of Thread Interrupt will still apply). By contrast, if the auto-execute section turns on Critical but never turns it off, every newly launched thread (such as a hotkey, custom menu item, or timed subroutine) starts off critical.

The command Thread NoTimers is similar to Critical except that it only prevents interruptions from timers.

[v1.0.47+]: Turning on Critical also puts SetBatchLines -1 into effect for the current thread.

Message Check Interval

[v1.0.47+]: Specifying a positive number as the first parameter (e.g. Critical 30) turns on Critical but also changes the minimum number of milliseconds between checks of the internal message queue. If unspecified, the default is 16 milliseconds while Critical is On, and 5 ms while Critical is Off. Increasing the interval postpones the arrival of messages/events, which gives the current thread more time to finish. This reduces the possibility that certain OnMessage() and GUI events will be lost due to "thread already running". However, commands that wait such as Sleep and WinWait will check messages regardless of this setting (a workaround is DllCall("Sleep", "UInt", 500)).

Because the system tick count generally has a granularity of 15.6 milliseconds, the minimum delta value is generally at least 15 or 16. The duration since the last check must exceed the interval setting in order for another check to occur. For example, a setting of 16 requires the tick count to change by 17 or greater. As a message check could occur at any time within the 15.6 millisecond window, any setting between 1 and 16 could permit two message checks within a single interval.

Note: Increasing the message-check interval too much may reduce the responsiveness of various events such as GUI window repainting.

Thread (command), Threads, #MaxThreadsPerHotkey, #MaxThreadsBuffer, OnMessage(), RegisterCallback(), Hotkey, Menu, SetTimer

Examples

Press a hotkey to display a tooltip for 3 seconds. Due to Critical, any new thread that is launched during this time (e.g. by pressing the hotkey again) will be postponed until the tooltip disappears.

#space::  ; Win+Space hotkey.
Critical
ToolTip No new threads will launch until after this ToolTip disappears.
Sleep 3000
ToolTip  ; Turn off the tip.
return  ; Returning from a hotkey subroutine ends the thread. Any underlying thread to be resumed is noncritical by definition.