I’m having some difficulty using the API function QueueUserAPC. Basically, I created a new thread that just loops using WaitForSingleObject on an Event object I created. I wanted this thread to sort of be a ‘lone’ worker thread that I can assign random function calls to that I need processed asynchronously..
However, when I create the thread, the only time QueueUserAPC will work is directly after the thread is created. If I insert say, Sleep(2000); right after CreateThread, then place QueueUserAPC after Sleep, it will not execute the function, even though it works if I do it before Sleep (right away).
I’ve tried using a different handle to the thread, i’ve tried resetting the event object, checked returned value of QueueUserAPC when it doesn’t work (it equals 1, which MSDN says non-zero result is successful operation).
I don’t quite know what to do about this. Any suggestions would be greatly appreciated.
Thanks!
J.R.
Use WaitForSingleObjectEx(). Unless one of the ‘Ex’ alertable wait calls is used, the APC will not be dequeued and run by the thread. If the wait returns WAIT_IO_COMPLETION – an APC has been executed by the thread. Typically, some kind of loop is used to ignore that completion: