sapi_windows_set_ctrl_handler Set or remove a CTRL event handler

发表日期:2021-07-01 10:15:43 | 来源: | | 浏览(711) 分类:杂项 函数

sapi_windows_set_ctrl_handler

(PHP 7 >= 7.4.0, PHP 8)

sapi_windows_set_ctrl_handlerSet or remove a CTRL event handler

说明

sapi_windows_set_ctrl_handler(callable $callable, bool $add = true): bool

Sets or removes a CTRL event handler, which allows Windows CLI processes to intercept or ignore CTRL+C and CTRL+BREAK events. Note that in multithreaded environments, this is only possible when called from the main thread.

参数

callable

A callback function to set or remove. If set, this function will be called whenever a CTRL+C or CTRL+BREAK event occurs. The function is supposed to have the following signature:

handler(int $event): void
event
The CTRL event which has been received; either PHP_WINDOWS_EVENT_CTRL_C or PHP_WINDOWS_EVENT_CTRL_BREAK.
Setting a null callable causes the process to ignore CTRL+C events, but not CTRL+BREAK events.

add

If true, the handler is set. If false, the handler is removed.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Basic sapi_windows_set_ctrl_handler() Usage

This example shows how to intercept CTRL events.

<?php 
function ctrl_handler(int $event){
    switch ($event) {
        case PHP_WINDOWS_EVENT_CTRL_C:            echo "You have pressed CTRL+C\n";
            break;
        case PHP_WINDOWS_EVENT_CTRL_BREAK:            echo "You have pressed CTRL+BREAK\n";
            break;
    }
}
sapi_windows_set_ctrl_handler('ctrl_handler');
while (true);
 // infinite loop, so the handler can be triggered?>

参见

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。