The WaitForEvent method suspends the calling thread
until the specified event is raised or the timeout interval expires.
While waiting, this method continues to process the thread's Windows
message queue. This ensures that the user interface remains responsive
and that event callbacks are able to execute on the correct thread.
Because messages are dispatched while this method blocks, application
code must be written with the possibility of reentrancy in mind.
That is, another message handler or event callback may run before
WaitForEvent returns. If your application maintains shared
state, you should use appropriate synchronization (critical sections,
mutexes, etc.) to ensure it remains consistent.
This method should generally be called from the same thread that
created the browser instance. Calling it from a worker thread that does
not own a message loop may prevent events from being delivered. If you
require a fully non-blocking design, you should use the event callback
function specified when creating or attaching the browser window, rather
than waiting synchronously.
You should not call WaitForEvent from within an event
handler. Doing so can result in unexpected behavior, as the method will
attempt to wait for additional events and dispatch messages while another
event is already being processed. This may lead to subtle reentrancy
issues or even deadlocks depending on the event ordering. Event handlers
should return promptly, and synchronous waits should be performed
only outside of callback contexts.
Note that if dwMilliseconds is set to INFINITE,
the method will not return until the event occurs. To avoid unresponsive
behavior, it is recommended you use a finite timeout value unless you are
certain the event will always be signaled within a reasonable period.