# Critical

Prevents the current thread from being interrupted by other threads.

    Critical [, Off]
    Critical 50 ; See bottom of remarks.

If the first parameter is omitted (or the word On), the current thread is made critical, meaning that it cannot be interrupted by another thread. If the first parameter is the word Off (or the number 0 in v1.0.48+), the current thread immediately becomes interruptible, regardless of the settings of "Thread Interrupt".

Unlike high-priority threads, events that occur during a critical thread are not discarded. For example, if the user presses a hotkey while the current thread is critical, the hotkey is buffered indefinitely until the current thread finishes or becomes noncritical, at which time the hotkey is launched as a new thread.

A critical thread will be interrupted in emergencies. Emergencies consist of: 1) the OnExit subroutine; 2) any OnMessage() function that monitors a message number less than 0x312 (or a callback triggered by such a message); and 3) any callback indirectly triggered by the critical thread itself (e.g. via SendMessage or DllCall). To avoid these interruptions, temporarily disable such functions.

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.

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

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.

In v1.0.47+, turning on Critical also puts SetBatchLines -1 into effect for the current thread.

In v1.0.47+, specifying a positive number as the first parameter (e.g. Critical 30) turns on Critical but also changes the number of milliseconds between checks of the internal message queue. If unspecified, messages are checked every 16 milliseconds while Critical is On, and every 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)). Note: Increasing the message-check interval too much may reduce the responsiveness of various events such as GUI window repainting.

### Related

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

### Example

    #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.