简体   繁体   中英

pause in cmd \ Freeze and UnFreeze

I need a command in cmd that works like pause but I can code to continue. 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. I want to continue cmd with some command.

Don't ever use system() if you can avoid it. It's crude, error-prone, and non-portable.

C11 introduces threading support, including 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. That standard is almost four years old now.

WinAPI defines the Sleep() function:

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.

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.).

I think what you're looking for is a method to check if stdin contains data ready to read; 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.

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 . 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 ):

#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. Other alternatives include using asynchronous I/O or extra threads. You should probably use non-blocking I/O or asynchronous I/O (ie epoll or kqueue ) for this task in particular; using extra threads just to determine when a character is sent to stdin is likely a little bit too hefty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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