- // event loops are not recursive, you need to create another loop!
- wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
-
- // SendIdleMessage() and Dispatch() below may throw so the code here should
- // be exception-safe, hence we must use local objects for all actions we
- // should undo
- wxEventLoopActivator activate(&ms_activeLoop, this);
- wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl);
-
- // we must ensure that OnExit() is called even if an exception is thrown
- // from inside Dispatch() but we must call it from Exit() in normal
- // situations because it is supposed to be called synchronously,
- // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
- // something similar here)
-#if wxUSE_EXCEPTIONS
- bool retryAfterException;
- do {
- retryAfterException=false;
-#endif
- wxTRY
- {
- for ( ;; )
- {
- #if wxUSE_THREADS
- wxMutexGuiLeaveOrEnter();
- #endif // wxUSE_THREADS
-
- // generate and process idle events for as long as we don't have
- // anything else to do
- while ( !Pending() && m_impl->SendIdleMessage() )
- ;
-
- // if the "should exit" flag is set, the loop should terminate but
- // not before processing any remaining messages so while Pending()
- // returns true, do process them
- if ( m_impl->ShouldExit() )
- {
- while ( Pending() )
- Dispatch();
-
- break;
- }
-
- // a message came or no more idle processing to do, sit in
- // Dispatch() waiting for the next message
- if ( !Dispatch() )
- {
- // we got WM_QUIT
- break;
- }
- }
- }
- wxCATCH_ALL(
- switch (m_impl->OnCatchAll()) {
- case wxEventLoopImpl::catch_continue:
- retryAfterException=true;
- break;
- case wxEventLoopImpl::catch_exit:
- OnExit();
- break;
- case wxEventLoopImpl::catch_rethrow:
- OnExit();
- // should be replaced with wx macro, but
- // there is none yet. OTOH, wxCATCH_ALL isn't
- // expanded unless wxUSE_EXCEPTIONS, so its
- // safe to use throw here.
- throw;
- break;
- }
- )
-#if wxUSE_EXCEPTIONS
- } while (retryAfterException);
-#endif
-
- return m_impl->GetExitCode();