+wxThreadError wxThread::Delete(ExitCode *pRc)
+{
+ ExitCode rc = 0;
+
+ // Delete() is always safe to call, so consider all possible states
+
+ // has the thread started to run?
+ bool shouldResume = FALSE;
+
+ {
+ wxCriticalSectionLocker lock(m_critsect);
+
+ if ( m_internal->GetState() == STATE_NEW )
+ {
+ // WinThreadStart() will see it and terminate immediately
+ m_internal->SetState(STATE_EXITED);
+
+ shouldResume = TRUE;
+ }
+ }
+
+ // is the thread paused?
+ if ( shouldResume || IsPaused() )
+ Resume();
+
+ HANDLE hThread = m_internal->GetHandle();
+
+ // does is still run?
+ if ( IsRunning() )
+ {
+ if ( IsMain() )
+ {
+ // set flag for wxIsWaitingForThread()
+ gs_waitingForThread = TRUE;
+
+#if wxUSE_GUI
+ wxBeginBusyCursor();
+#endif // wxUSE_GUI
+ }
+
+ // ask the thread to terminate
+ {
+ wxCriticalSectionLocker lock(m_critsect);
+
+ m_internal->Cancel();
+ }
+
+#if wxUSE_GUI
+ // we can't just wait for the thread to terminate because it might be
+ // calling some GUI functions and so it will never terminate before we
+ // process the Windows messages that result from these functions
+ DWORD result;
+ do
+ {
+ result = ::MsgWaitForMultipleObjects
+ (
+ 1, // number of objects to wait for
+ &hThread, // the objects
+ FALSE, // don't wait for all objects
+ INFINITE, // no timeout
+ QS_ALLEVENTS // return as soon as there are any events
+ );
+
+ switch ( result )
+ {
+ case 0xFFFFFFFF:
+ // error
+ wxLogSysError(_("Can not wait for thread termination"));
+ Kill();
+ return wxTHREAD_KILLED;
+
+ case WAIT_OBJECT_0:
+ // thread we're waiting for terminated
+ break;
+
+ case WAIT_OBJECT_0 + 1:
+ // new message arrived, process it
+ if ( !wxTheApp->DoMessage() )
+ {
+ // WM_QUIT received: kill the thread
+ Kill();
+
+ return wxTHREAD_KILLED;
+ }
+
+ if ( IsMain() )
+ {
+ // give the thread we're waiting for chance to exit
+ // from the GUI call it might have been in
+ if ( (gs_nWaitingForGui > 0) && wxGuiOwnedByMainThread() )
+ {
+ wxMutexGuiLeave();
+ }
+ }
+
+ break;
+
+ default:
+ wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
+ }
+ } while ( result != WAIT_OBJECT_0 );
+#else // !wxUSE_GUI
+ // simply wait for the thread to terminate
+ //
+ // OTOH, even console apps create windows (in wxExecute, for WinSock
+ // &c), so may be use MsgWaitForMultipleObject() too here?
+ if ( WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0 )
+ {
+ wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
+ }
+#endif // wxUSE_GUI/!wxUSE_GUI
+
+ if ( IsMain() )
+ {
+ gs_waitingForThread = FALSE;
+
+#if wxUSE_GUI
+ wxEndBusyCursor();
+#endif // wxUSE_GUI
+ }
+ }
+
+ if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) )
+ {
+ wxLogLastError("GetExitCodeThread");
+
+ rc = (ExitCode)-1;
+ }
+
+ if ( IsDetached() )
+ {
+ // if the thread exits normally, this is done in WinThreadStart, but in
+ // this case it would have been too early because
+ // MsgWaitForMultipleObject() would fail if the therad handle was
+ // closed while we were waiting on it, so we must do it here
+ delete this;
+ }
+
+ wxASSERT_MSG( (DWORD)rc != STILL_ACTIVE,
+ wxT("thread must be already terminated.") );
+
+ if ( pRc )
+ *pRc = rc;
+
+ return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR;
+}
+
+wxThreadError wxThread::Kill()
+{
+ if ( !IsRunning() )
+ return wxTHREAD_NOT_RUNNING;
+
+ if ( !::TerminateThread(m_internal->GetHandle(), (DWORD)-1) )
+ {
+ wxLogSysError(_("Couldn't terminate thread"));
+
+ return wxTHREAD_MISC_ERROR;
+ }
+
+ m_internal->Free();
+
+ if ( IsDetached() )
+ {
+ delete this;
+ }
+
+ return wxTHREAD_NO_ERROR;
+}
+
+void wxThread::Exit(ExitCode status)
+{
+ m_internal->Free();
+
+ if ( IsDetached() )
+ {
+ delete this;
+ }
+
+#if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500))
+ _endthreadex((unsigned)status);
+#else // !VC++
+ ::ExitThread((DWORD)status);
+#endif // VC++/!VC++
+
+ wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
+}
+
+// priority setting
+// ----------------
+
+void wxThread::SetPriority(unsigned int prio)
+{
+ wxCriticalSectionLocker lock(m_critsect);
+
+ m_internal->SetPriority(prio);
+}
+
+unsigned int wxThread::GetPriority() const
+{
+ wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
+
+ return m_internal->GetPriority();
+}
+
+unsigned long wxThread::GetId() const
+{
+ wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
+
+ return (unsigned long)m_internal->GetId();
+}
+
+bool wxThread::IsRunning() const
+{
+ wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
+
+ return m_internal->GetState() == STATE_RUNNING;
+}
+
+bool wxThread::IsAlive() const