+
+// create/start thread
+// -------------------
+
+wxThreadError wxThread::Create(unsigned int stackSize)
+{
+ wxCriticalSectionLocker lock(m_critsect);
+
+ if ( !m_internal->Create(this, stackSize) )
+ return wxTHREAD_NO_RESOURCE;
+
+ return wxTHREAD_NO_ERROR;
+}
+
+wxThreadError wxThread::Run()
+{
+ wxCriticalSectionLocker lock(m_critsect);
+
+ if ( m_internal->GetState() != STATE_NEW )
+ {
+ // actually, it may be almost any state at all, not only STATE_RUNNING
+ return wxTHREAD_RUNNING;
+ }
+
+ // the thread has just been created and is still suspended - let it run
+ return Resume();
+}
+
+// suspend/resume thread
+// ---------------------
+
+wxThreadError wxThread::Pause()
+{
+ wxCriticalSectionLocker lock(m_critsect);
+
+ return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
+}
+
+wxThreadError wxThread::Resume()
+{
+ wxCriticalSectionLocker lock(m_critsect);
+
+ return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
+}
+
+// stopping thread
+// ---------------
+
+wxThread::ExitCode wxThread::Wait()
+{
+ // although under MacOS we can wait for any thread, it's an error to
+ // wait for a detached one in wxWin API
+ wxCHECK_MSG( !IsDetached(), (ExitCode)-1,
+ _T("can't wait for detached thread") );
+
+ ExitCode rc = (ExitCode)-1;
+
+ (void)Delete(&rc);
+
+ m_internal->Free();
+
+ return rc;
+}
+
+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();
+
+ // 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
+ // simply wait for the thread to terminate
+ while( TestDestroy() )
+ {
+ ::YieldToAnyThread() ;
+ }
+#else // !wxUSE_GUI
+ // simply wait for the thread to terminate
+ while( TestDestroy() )
+ {
+ ::YieldToAnyThread() ;
+ }
+#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;
+ }
+
+ m_internal->SetResult( status ) ;
+
+/*
+#if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500))
+ _endthreadex((unsigned)status);
+#else // !VC++
+ ::ExitThread((DWORD)status);
+#endif // VC++/!VC++