ncurses timeout() is terminating my program

31 views Asked by At

I'm receiving input in a timed loop, and getch() blocks (waits for key) the program even though the loop has ended. When utilizing timeout(), or other functions, it doesn't block, but terminates the entire program.

initscr();
raw();
noecho();

while(time(NULL)-initTime < maxtime)
{
    timeout(3); // 3 as an example
    getch();
}
// do stuff

refresh();
// hit a key to exit
getch();
endwin();

That seems not useful, so I must be missing something. How to non-block (e.g within 3 seconds), and continue executing my code?

1

There are 1 answers

0
ayuval On

As also stated in the comments, the problem is with setting timeout only once, and thus apply it to the whole program.

The solution:

while(time(NULL)-initTime < maxtime)
{
    timeout(3); // 3 as an example
    getch();
}
timeout(-1); // The rest of the getch() calls can block now