Non-blocking io function instead of cin.get ()

I have a C ++ console application that can be interrupted by pressing any key (implemented using kbhit ()), in which case it prints what it did and exits (or can be left to shut down (some entry ) and still prints feedback). I am making an aC # gui application for this console application (I am calling an exe file from a C # form), and instead of pressing any key on the keyboard, I use the "stop" button, which sends Environment.NewLine (or "\ n") to the redirected input and in a C ++ application now I need to use something other than kbhit () to work it. I tried using cin.get (), for example:

if (kbhit() || (cin.get() > 0))  
    stop_recording=true;  

but cin.get () is a blocking function, and if I want the console application to run to the end, it does not close without pressing the stop button. Is there any function like cin.get () that doesn't actually block, or maybe another way to send "any key" from C # to my console application process?

+3
source share
3 answers

use _getch()insted cin.get(). his command is blocking, but having completed it only if ( _kbhit() ) he will immediately return.

0
source

You can try sending Ctrl + C to the console process to cause it to be violated.

, - Mutex/Semaphore ( ) . ( kbhit), Mutex/Semaphore , .

, @Komyg, .

, IPC " ", .

0

: ++. .
boost- ++, boost. , :

bool stopped=false;

void inputSent() //
{  
    if (kbhit() || (cin.get() > 0))  
        stopped=true;  

}  

and then in main.cpp:

boost::thread stoppedThread = boost::thread(inputSent);  

After that, I just check if == true was stopped, and I do not use stopThread.join (I do not know if this error was or not, since I do not know when the program will end if I leave the zombie threads behind .. maybe someone could tell me about this).

0
source

All Articles