c - ncurses 带有信号的 getch() 行为
问题描述
我的代码设置了一个计时器,它每 x 秒发送一次 SIG_ALRM。然后它进入一个调用 getch() 的输入处理循环。
int total_keys = 0;
while (1) {
inputchar = wgetch(mywindow);
mvprintw(LINES - 2, 2, "%d", total_keys++);
refresh();
switch (inputchar) {
...
}
}
由于我将 getch() 设置为阻塞 ( wtimeout(mywindow, -1);
),因此我希望 total_keys 仅在我按下一个键时才会上升,但我发现每次收到 SIG_ALRM 时,getch() 都会返回并且 total_keys 会增加。有谁知道为什么会这样?
编辑:这是我的 SIG_ALRM 处理程序
void alarm_handler(int signum, siginfo_t *si, void *ucontext) {
timer_t *timeridp = si->si_value.sival_ptr;
if (*timeridp == *update_timerp) {
update();
}
}
解决方案
检查是否有错误返回,发生这种情况时不要处理输入。
while (1) {
inputchar = wgetch(mywindow);
if (inputchar == ERR) {
if (errno == EINTR) {
continue;
} else {
// report failure somehow
}
}
mvprintw(LINES - 2, 2, "%d", ++total_keys);
refresh();
switch (inputchar) {
...
}
}
推荐阅读
- android - 当我将标签从一个切换到另一个时,片段中的 Webview 总是刷新
- java - 在运行前定义 Maven 依赖版本
- html - 为什么我的嵌套 CSS 过渡不起作用?
- excel - 如何使用 Excel VBA 将具有属性的 xml 映射 (xsd) 项绑定到工作表单元格?
- browser - 搜索插件 XML 中是否可以有相对路径?(开放搜索)
- python-2.7 - 无法使用 python 2.7 安装数据科学 - ValueError: This package requires python >= 3.0
- swift - 图像未在 Alamofire 中使用 multipartformdata 上传到服务器
- kotlin - 作业问题 - 我做错了什么?
- java - 异常是 java.net.ProtocolException:无效的 HTTP 方法:PATCH
- html - 在没有浮动的情况下将元素彼此相邻定位不能正确排列在顶部