Yes, that is pretty high. I pulled pieces of code for the
illustration from multiple sources. I'm sure the OP or anyone doing
this would adjust it. And yes if you are going to with for multiple
events to be signaled, using a multi object wait function is ideal.
On Sat, 27 Mar 2010 20:54:01 -0400, Hector Santos
<sant9442@nospam.gmail.com> wrote:
So in the LOOP I provided, it will change to to this:
hExitEvent = CreateEvent(0, TRUE, FALSE, 0);
SetConsoleCtrlHandler((PHANDLER_ROUTINE)&ConsoleHandler,TRUE);
_cprint("* Press ESC to exit\n");
while (WaitForSingleObject(hExitEvent,100) != WAIT_OBJECT_0) {
if (_kbhit() && _getch() == 27) break;
MSG msg;
while (::PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
Now you don't need the Sleep(75) because call to:
WaitForSingleObject(hExitEvent,100)
is the most *efficient* form of synchronization and waiting for events
to occur in Windows!
That pauses your thread by 100 msec each time around the loop when the
event isn't signaled, which is almost all the time. You can avoid polling
altogether by using MsgWaitForMultipleObjects. For an example, see my web
page:
http://members.cox.net/doug_web/threads.htm#Q6
Also, I'd look for WM_KEYDOWN instead of using _kbhit. Consider what
happens if ESC comes in between _kbhit and PeekMessage.