简体   繁体   English

在cmd中暂停\\冻结和取消冻结

[英]pause in cmd \ Freeze and UnFreeze

I need a command in cmd that works like pause but I can code to continue. 我需要cmd中的命令,该命令的作用类似于暂停,但我可以编写代码以继续。 eg 例如

system("pause");
some lines of code;` 

The problem with system("pause") is that "some lines of code" will not work until the user press sth. system("pause")在于,直到用户按下sth,“某些代码行”才起作用。 I want to continue cmd with some command. 我想用一些命令继续执行cmd。

Don't ever use system() if you can avoid it. 如果可以避免,请勿使用system() It's crude, error-prone, and non-portable. 它很粗糙,容易出错且不可移植。

C11 introduces threading support, including thrd_sleep() . C11引入了线程支持,包括thrd_sleep() That should be your preferred solution (if supported by your compiler setup). 那应该是您的首选解决方案(如果编译器安装程序支持)。

If your compiler vendor does not support C11, bugger him about it. 如果您的编译器供应商支持C11,管不着他。 That standard is almost four years old now. 那个标准现在已经快四年了。

WinAPI defines the Sleep() function: 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. 此函数使线程放弃其剩余时间片,并在基于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. 我想要一些可以运行代码但仅在我授予其权限时才更新cmd的代码。

If I understand correctly, the code shall produce output which you don't want to be shown before you press a key. 如果我理解正确, 那么代码将产生您不希望在按下键之前显示的输出。 If you don't mind to have the output paged, you could use something like 如果您不介意分页输出,则可以使用类似

        FILE *stream = popen("PAUSE<CON&&MORE", "w");

and let the code output to stream (with fprintf(stream, ...) etc.). 并让代码输出流(使用fprintf(stream, ...)等)。

I think what you're looking for is a method to check if stdin contains data ready to read; 我认为您正在寻找的是一种检查stdin包含准备读取的数据的方法。 you want to use some non-blocking or asynchronous I/O so that you can read input when it becomes available, and perform other tasks until then. 您想使用一些非阻塞异步 I / O,以便您可以在可用时读取输入,并在此之前执行其他任务。

You won't find a whole heap about non-blocking/asynchronous I/O in standard C, but in POSIX C you can set STDIN_FILENO as non-blocking using fcntl . 您不会在标准C中找到有关非阻塞/异步I / O的全部内容,但是在POSIX C中,您可以使用fcntlSTDIN_FILENO设置为非阻塞。 As an example, here's a program which prompts you to press enter (like pause does) and busy-loops, allowing your code to conduct other (preferably non-blocking) actions inside the loop while it waits for the keystroke (ahemm, byte, since stdin is technically a file ): 举例来说,这是一个程序,提示您按Enter键(如pause操作)和忙循环,从而让您的代码在等待按键(ahemm,byte,由于从技术上讲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 */
    }
}

That's non-blocking I/O. 那是非阻塞的I / O。 Other alternatives include using asynchronous I/O or extra threads. 其他选择包括使用异步I / O或额外的线程。 You should probably use non-blocking I/O or asynchronous I/O (ie epoll or kqueue ) for this task in particular; 您可能应该为此任务特别使用非阻塞I / O或异步I / O(即epollkqueue ); using extra threads just to determine when a character is sent to stdin is likely a little bit too hefty. 仅使用额外的线程来确定何时将字符发送到stdin可能太重了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM