Briefly, my application contains a main UI thread, and an internal event loop. I overrode CWinApp::OnIdle to run my own event loop, it looks like,

Code:
BOOL CMyApp::OnIdle(LONG lCount)
{
    // TODO: Add your specialized code here and/or call the base class
   
    if (CWinApp::OnIdle(lCount))
        return TRUE;

    if (mController)
{
		// run my own event loop
        mController->run();
    }

    // Tell framework we don't need any more idle time to avoid CPU overhead.
    return FALSE;
}
The controller::run simply checked out if there’s any internal event and processes it accordingly.

The problem has occurred when there’s a http request. The use case was,
1. if there’s a http request, I should process it via WinInet.
2. WinInet will create a new thread in background, here allow me to call this thread as “Http thread”, to get data downloaded or do whatever it’s supposed to do. When it’s done with the job, it should call a callback which I supplied in step 1.
3. In the callback, it posted an internal event to internal event queue, and ideally, controller should immediately process it.

The point is that the main UI thread possibly goes into idle state after it started a http request, and OnIdle then never gets invoked if there’s no UI messages to awake UI thread. This resulted in that controller didn’t process our own event at all even if we already notified it http request done.

I thought there’s a simple solution that posted a WM_NULL to main UI thread in http callback, but it didn’t perfectly work in my case, since
1. In callback, actually in that http thread, AfxGetMainWnd returned NULL, so I can’t get main window handle and got nowhere to post WM_NULL.
2. For portable issue, I’m not allowed to preserve a global hwnd referring to main window.

Besides, I’m thinking about improve my event processing, e.g., fully control when and where to process events instead of using OnIdle, use some asynchronous measurement to avoid high CPU occupation. However, it rose up another problem, if my thread get blocked and is waiting for a http request, how could I promptly respond to UI messages?

Any idea?