在 cmd \ 冻结和解冻中暂停
pause in cmd \ Freeze and UnFreeze
我需要在 cmd 中使用类似于暂停的命令,但我可以编写代码以继续。
例如
system("pause");
some lines of code;`
system("pause")
的问题是 "some lines of code" 在用户按下某物之前不会工作。
我想用一些命令继续 cmd。
如果可以避免,永远不要使用 system()
。它简陋、容易出错且不可移植。
C11 引入了线程支持,包括thrd_sleep()。那应该是您的首选解决方案(如果您的编译器设置支持)。
如果您的编译器供应商不支持 C11,请打扰他。该标准现在已经快四年了。
WinAPI 定义了 Sleep()
函数:
VOID WINAPI Sleep(
_In_ DWORD dwMilliseconds
);
This function causes a thread to relinquish the remainder of its time
slice and become unrunnable for an interval based on the value of
dwMilliseconds.
#include <windows.h>
int main()
{
Sleep( 5000 ); // pause execution for at least 5 seconds
some_lines_of_code;
return 0;
}
I want something that run the code but update cmd only when I give the
permission to it.
如果我理解正确,代码 将产生您不希望在您按下某个键之前显示的输出。如果你不介意让输出分页,你可以使用像
这样的东西
FILE *stream = popen("PAUSE<CON&&MORE", "w");
并让代码输出到流(使用fprintf(stream, ...)
等)。
我认为您正在寻找的是一种检查 stdin
是否包含可供读取的数据的方法;你想使用一些 非阻塞 或 异步 I/O 以便你可以在输入可用时读取输入,并执行其他任务到那时。
你不会在标准 C 中找到关于 non-blocking/asynchronous I/O 的整个堆,但在 POSIX C 中你可以使用 STDIN_FILENO
设置为非阻塞=13=]。例如,这里有一个程序会提示您按回车键(就像 pause
那样)和忙循环,允许您的代码在等待击键的同时在循环内执行其他(最好是非阻塞)操作(咳咳,字节,因为 stdin
在技术上是一个 文件 ):
#include <stdio.h>
#include <fcntl.h>
int main(void) {
char c;
puts("Press any key to continue...");
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
while (read(STDIN_FILENO, 1, &c) != 1 && errno == EAGAIN) {
/* code in here will execute repeatedly until a key is struck or a byte is sent */
errno = 0;
}
if (errno) {
/* code down here will execute when an input error occurs */
}
else {
/* code down here will execute when that precious byte is finally sent */
}
}
这是非阻塞的 I/O。其他替代方案包括使用异步 I/O 或额外线程。您可能应该特别使用非阻塞 I/O 或异步 I/O(即 epoll
或 kqueue
)来完成此任务;使用额外的线程来确定何时将字符发送到 stdin
可能有点太重了。
我需要在 cmd 中使用类似于暂停的命令,但我可以编写代码以继续。 例如
system("pause");
some lines of code;`
system("pause")
的问题是 "some lines of code" 在用户按下某物之前不会工作。
我想用一些命令继续 cmd。
如果可以避免,永远不要使用 system()
。它简陋、容易出错且不可移植。
C11 引入了线程支持,包括thrd_sleep()。那应该是您的首选解决方案(如果您的编译器设置支持)。
如果您的编译器供应商不支持 C11,请打扰他。该标准现在已经快四年了。
WinAPI 定义了 Sleep()
函数:
VOID WINAPI Sleep(
_In_ DWORD dwMilliseconds
);
This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds.
#include <windows.h>
int main()
{
Sleep( 5000 ); // pause execution for at least 5 seconds
some_lines_of_code;
return 0;
}
I want something that run the code but update cmd only when I give the permission to it.
如果我理解正确,代码 将产生您不希望在您按下某个键之前显示的输出。如果你不介意让输出分页,你可以使用像
这样的东西 FILE *stream = popen("PAUSE<CON&&MORE", "w");
并让代码输出到流(使用fprintf(stream, ...)
等)。
我认为您正在寻找的是一种检查 stdin
是否包含可供读取的数据的方法;你想使用一些 非阻塞 或 异步 I/O 以便你可以在输入可用时读取输入,并执行其他任务到那时。
你不会在标准 C 中找到关于 non-blocking/asynchronous I/O 的整个堆,但在 POSIX C 中你可以使用 STDIN_FILENO
设置为非阻塞=13=]。例如,这里有一个程序会提示您按回车键(就像 pause
那样)和忙循环,允许您的代码在等待击键的同时在循环内执行其他(最好是非阻塞)操作(咳咳,字节,因为 stdin
在技术上是一个 文件 ):
#include <stdio.h>
#include <fcntl.h>
int main(void) {
char c;
puts("Press any key to continue...");
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
while (read(STDIN_FILENO, 1, &c) != 1 && errno == EAGAIN) {
/* code in here will execute repeatedly until a key is struck or a byte is sent */
errno = 0;
}
if (errno) {
/* code down here will execute when an input error occurs */
}
else {
/* code down here will execute when that precious byte is finally sent */
}
}
这是非阻塞的 I/O。其他替代方案包括使用异步 I/O 或额外线程。您可能应该特别使用非阻塞 I/O 或异步 I/O(即 epoll
或 kqueue
)来完成此任务;使用额外的线程来确定何时将字符发送到 stdin
可能有点太重了。