开发者

detect the end of multiple async callbacks

I have some code that collects all the window handles into an array. The problem is, since the callbacks are done asynchronously, i dont know when the enumeration is actually finished. What is the best approach to solving this? I assume this is a common problem in windows programming.

BOOL CALLBACK enumWindowsProc3(HWND hwnd, // handle to parent window开发者_StackOverflow社区
        LPARAM lParam // application-defined value
) {

    //add hwnd to array

    return TRUE;
}

int howManyWindows() {

    EnumWindows (enumWindowsProc3, 0);      

    // need to wait here for EnumWindows() to finish...

    return array size
}


I'm not a Windows API expert, but this is a general programming problem.

If you have access to a semaphore type structure that can wait(),signal(),and give you a count of items, then you can block the thread until a certain count is reached. Each asynchronous query to the window would then signal() to the semaphore.

Otherwise you can do a general poll checking the completion of all items but make sure that the thread goes to sleep for a certain amount of time while in the poll loop.


EnumWindows callbacks are synchronous, not async. No wait is required. Code after EnumWindows will execute only after all callbacks have been completed...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜