Critical

防止当前线程被其他线程中断, 或开启可中断.

Critical [, Off]
Critical 50 ; 请参阅备注底部

如果首个参数省略 (或为单词 On), 则设置 当前线程 为关键的, 这意味着它不会被其他线程中断. 如果首个参数为 Off(或在 v1.0.48+ 为数字 0),则当前线程会不论 Thread Interrupt 设置如何都会变成可中断状态。

Behavior of Critical Threads

高优先级 线程不同, 在关键线程中产生的事件不会被丢弃. 例如, 在当前线程为关键线程时用户按下了 热键, 那么此热键会被缓冲到当前线程结束或成为非关键线程时才作为新的线程启动.

关键线程也会被紧急事件中断. 紧急事件包括: 1) OnExit 子程序; 2) 监听消息号小于 0x312 的任何 OnMessage() 函数 (或被这些消息触发的 回调); 和 3) 由紧急线程自身间接触发的任何 回调 (例如通过 SendMessageDllCall). 要避免被这些事件中断, 可以临时禁用这些函数.

关键线程在显示 MsgBox 或其他对话框时, 会变成可中断的. 但是,与 Thread Interrupt 不同,在用户解除对话框后这个线程会再恢复为关键的。

Critical Off

当缓冲的事件等待启动新的线程时, 使用 Critical Off 不会导致立即中断当前线程. 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+,打开 Critical 的同时也会让 SetBatchLines -1当前线程生效。

Message Check Interval

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.

相关

Thread(命令), 线程, #MaxThreadsPerHotkey, #MaxThreadsBuffer, OnMessage(), RegisterCallback(), Hotkey, Menu, SetTimer

示例

#space::  ; Win+Space 热键.
Critical
ToolTip No new threads will launch until after this ToolTip disappears.
Sleep 3000
ToolTip  ; 关闭提示.
return  ; 从热键子程序中返回, 这样结束了当前线程. 后面的线程会按设置恢复为非关键的.