[英]How to interrupt a C++ program
This is the gist the program : 这是要点程序:
while(true)
{
//bunch of codes that gets data from port ,
//if there is no data it waits here .
}
i am using linux , is there any inbuilt support for keypresses like Ctrl+C . 我正在使用linux,对Ctrl + C之类的按键是否有内置支持。 I can catch that using signal(SIGINT, signal_callback_handler);
我可以使用signal(SIGINT, signal_callback_handler);
来捕获它signal(SIGINT, signal_callback_handler);
but Ctrl+C has some problem as it gives errors . 但是Ctrl + C存在一些问题,因为它给出了错误。
i want to get out of this loop on keypress , is this possible ? 我想退出按键的循环,这可能吗? If yes , how to do it . 如果是,怎么做。
while(!_kbhit()){
// do stuff here.
}
Works on Windows. 适用于Windows。
You can signal an interupt 你可以发出中断信号
#include <csignal>
raise(SIGINT);
or 要么
raise(SIGABRT);
The function that can terminate your program is exit()
. 可以终止程序的函数是exit()
。
To input a premature termination of your cicle you can use break
. 要输入cicle的过早终止,可以使用break
。
If you are willing to use some C oriented solution i suggest to take a look at this question . 如果您愿意使用一些面向C的解决方案,建议您看一下这个问题 。
For something strictly related to linux you probably should refer to the ncurses library in combination with the exit function . 对于与Linux严格相关的内容,您可能应该结合使用exit函数参考ncurses库 。
Also do not assume that a particular key combination is equal to a termination, many terminals are not real terminals but emulators, and many emulators are customizable, by the OS creator and by the user. 也不要假设特定的按键组合等于终止,许多终端不是真实终端而是模拟器,并且许多模拟器可由OS创建者和用户自定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.