+ wxGetApp().m_critsect.Enter();
+
+ // check if we have any threads running first
+ const wxArrayThread& threads = wxGetApp().m_threads;
+ size_t count = threads.GetCount();
+
+ if ( count )
+ {
+ // we do, ask them to stop
+ for ( size_t n = 0; n < count; n++ )
+ {
+ threads[n]->Delete();
+ }
+
+ // set the flag for MyThread::OnExit()
+ wxGetApp().m_waitingUntilAllDone = TRUE;
+ }
+
+ wxGetApp().m_critsect.Leave();
+
+ if ( count )
+ {
+ // now wait for them to really terminate but leave the GUI mutex
+ // before doing it as otherwise we might dead lock
+ wxMutexGuiLeave();
+
+ wxGetApp().m_condAllDone.Wait();
+
+ wxMutexGuiEnter();
+ }
+ //else: no threads to terminate, no condition to wait for