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): 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. Specifying 0 turns off Critical.

Behavior of Critical Threads

Critical threads are uninterruptible; for details, see Threads.

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.

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 Size 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 by the auto-execute thread, all threads start off as noncritical (though the settings of Thread "Interrupt" will still apply). By contrast, if the auto-execute thread 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 function Thread "NoTimers" is similar to Critical except that it only prevents interruptions from timers.

Message Check Interval

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, functions 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 (function), Threads, #MaxThreadsPerHotkey, #MaxThreadsBuffer, OnMessage, CallbackCreate, Hotkey, Menu object, SetTimer

Examples

#1

#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 function ends the thread. Any underlying thread to be resumed is noncritical by definition.
}