In a graphical application I execute debug commands using the console input. When the console is created a new thread is also created to gather the user commands that handles all that input, the graphical application continues running parallel. I use boost::thread library.
It works good so far, however I haven't found a nice solution to stop the execution of this thread. The thread is always waiting for a user input:
while(appRunning)
{
std::cin>>theUserCommand;
// ...do stuff
}
Then when the graphical application ends, it will stop all console functions, in which I include the thread:
appRunning = false;
// do some more related clean up
myListeningThread->join();
As you can see the std::cin will be waiting for user input, after the join has being called. One of the solutions I tried is to create events "synthetizing keystrokes", the std::cin will get whatever value you send with an ENTER, the thread will end nicely, this solutions is horrible and I don't want to keep it. Besides, it did work in one of the environments the tool is executed, but fails when I tried using it along with a UI API. Could you guys guide me how can I fix this in a correct way? Can't really say for sure if in the C++ documentation there is a function to stop std::cin waiting for user input, and just and continue the program execution, is it even possible?
EDIT: Fine I find that keybd_event is a bit misleading for some environments, explicitly specifying the input handler with the WriteConsoleInput works good.