]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxThread::OnKill() and OnDelete() callbacks.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 23 Oct 2010 14:10:12 +0000 (14:10 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 23 Oct 2010 14:10:12 +0000 (14:10 +0000)
Call OnXXX() from wxThread::Kill() and Delete() respectively to allow the
thread being terminated perform some cleanup.

Closes #9046.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65882 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/thread.h
interface/wx/thread.h
src/msw/thread.cpp
src/unix/threadpsx.cpp

index 4cb400ad3ed23790c96f748554a4a94758a8107a..bc7eaae2a2f822cca0d191c5cf5f7f5f0b391c2d 100644 (file)
@@ -406,6 +406,7 @@ Major new features in this release
 All:
 
 - Added "rest" argument to wxString::Before{First,Last}().
+- Added wxThread::OnKill() and OnDelete() callbacks.
 
 All (GUI):
 
index ed9efa05bfef4daee6be941c543c31253155244d..fbb3cfb04207f51599b542c98e61851f09138f34 100644 (file)
@@ -585,6 +585,19 @@ protected:
     // of this thread.
     virtual void *Entry() = 0;
 
+
+    // Callbacks which may be overridden by the derived class to perform some
+    // specific actions when the thread is deleted or killed. By default they
+    // do nothing.
+
+    // This one is called by Delete() before actually deleting the thread and
+    // is executed in the context of the thread that called Delete().
+    virtual void OnDelete() {}
+
+    // This one is called by Kill() before killing the thread and is executed
+    // in the context of the thread that called Kill().
+    virtual void OnKill() {}
+
 private:
     // no copy ctor/assignment operator
     wxThread(const wxThread&);
index 34d98462fdbe1b16bf89f26aa0031a3ac2ce1a95..52b94f3128992cc7bf6cffd8408ca5a4fa3442d5 100644 (file)
@@ -454,6 +454,37 @@ public:
     */
     virtual ExitCode Entry() = 0;
 
+    /**
+        Callback called by Delete() before actually deleting the thread.
+
+        This function can be overridden by the derived class to perform some
+        specific task when the thread is gracefully destroyed. Notice that it
+        will be executed in the context of the thread that called Delete() and
+        <b>not</b> in this thread's context.
+
+        TestDestroy() will be true for the thread before OnDelete() gets
+        executed.
+
+        @since 2.9.2
+
+        @see OnKill()
+    */
+    virtual void OnDelete();
+
+    /**
+        Callback called by Kill() before actually killing the thread.
+
+        This function can be overridden by the derived class to perform some
+        specific task when the thread is terminated. Notice that it will be
+        executed in the context of the thread that called Kill() and <b>not</b>
+        in this thread's context.
+
+        @since 2.9.2
+
+        @see OnDelete()
+    */
+    virtual void OnKill();
+
     /**
         @deprecated
         Use CreateThread() instead.
index 53c192cc5014d0f54468e1da065174ca61369f2d..1a668976f3f4d9597fbf527c15af06b6ae9de154 100644 (file)
@@ -686,6 +686,8 @@ bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
 
 wxThreadError wxThreadInternal::Kill()
 {
+    m_thread->OnKill();
+
     if ( !::TerminateThread(m_hThread, THREAD_ERROR_EXIT) )
     {
         wxLogSysError(_("Couldn't terminate thread"));
@@ -759,6 +761,7 @@ wxThreadInternal::WaitForTerminate(wxCriticalSection& cs,
         Cancel();
     }
 
+    threadToDelete->OnDelete();
 
     // now wait for thread to finish
     if ( wxThread::IsMain() )
index e818385c668b6b858aa47dd2b29a36b8175e78d4..ebb365b0d094d83b6d1df7d68fd147940f691a21 100644 (file)
@@ -1453,6 +1453,8 @@ wxThreadError wxThread::Delete(ExitCode *rc)
 
     m_critsect.Leave();
 
+    OnDelete();
+
     switch ( state )
     {
         case STATE_NEW:
@@ -1501,6 +1503,8 @@ wxThreadError wxThread::Kill()
     wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR,
                  wxT("a thread can't kill itself") );
 
+    OnKill();
+
     switch ( m_internal->GetState() )
     {
         case STATE_NEW: