wxThreadTimer

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
orbitcowboy
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Mon Jul 23, 2007 9:01 am

wxThreadTimer

Post by orbitcowboy »

I @all i've realized a Timer class based on Threads. The interfaces are similar to the well known wxTimer class.

wxThreadTimer.h:

Code: Select all


/*  
*******************************************************************
* CVS Concurrent Versions Control
* ----------------------------------------------------------------
* $RCSfile: wxThreadTimer.h $
* $Revision:$  
* ----------------------------------------------------------------
* $Author: Ettl Martin$
* $Date: $
* $Locker: $
* ----------------------------------------------------------------
* $Log:$
******************************************************************
*/
/*! \file
*   \brief This class supports the nearly the same functions than
*			the class wxTimer. It is realized with threads.\n
*
*
******************************************************************
*	Used precompiler definitions: 	<br>
*	Used namespaces: standard		<br>
*	Used exeption-handles:			<br>
*
*
*
******************************************************************/

#ifndef __wxThreadTimer_class__
#define __wxThreadTimer_class__

/// \brief Include wxWidgets headers
#include <wx/wx.h>
/// \brief Include wxThread headers 
#include <wx/thread.h>

// ---------------------------------------------------------------------------
/// \brief This class supports thread support and offers the same functions
/// like the well known wxTimer class
/// 
// ---------------------------------------------------------------------------
class wxThreadTimer : public wxThread
{

	public:

			// -----------------------------------
			/// \brief Ctor of class wxThreadTimer
			/// ----------------------------------
			wxThreadTimer();

			// -----------------------------------
			/// \brief Dtor of class wxThreadTimer
			/// ----------------------------------
			~wxThreadTimer();

			// ----------------------
			// Get and Set functions:
			// ----------------------

			// ------------------------------------------------------
			/// unsigned GetInterval(void)
			///
			/// this function returns the threadtimer interval
			/// 
			/// \param ---
			/// 
			/// \return the current timer interval [ms = milli seconds]
			// ------------------------------------------------------
			inline unsigned  GetInterval(void) const 				{return m_uiInterval 		;};


			// ------------------------------------------------------
			/// void SetInterval(const unsigned &new_interval))
			///
			/// change the interval value
			/// 
			/// \param uiInterval --> change the interval [ms = milli seconds]
			/// 
			/// \return ---
			// ------------------------------------------------------
			inline void SetInterval(const unsigned &uiInterval)		{m_uiInterval=uiInterval	;};


			// ------------------------------------------------------
			/// bool IsRunning(void) const
			///
			/// a function to check, the thread runs, or not
			/// 
			/// \param ---
			/// 
			/// \return true == running, false == not running
			// ------------------------------------------------------
			bool IsRunning(void) const  							{return m_bRunning	 		;};


			
			// ------------------------------------------------------
			/// virtual void  Notify()
			///
			/// overwrite this function in order to get periodically
			/// calls.
			/// 
			/// \param void
			/// 
			/// \return ---
			// ------------------------------------------------------
			virtual void  Notify(void)=0;


			// ------------------------------------------------------
			/// void Start(const unsigned &uiInterval = 1000)
			///
			/// Start the periodic call of the function Notify
			/// 
			/// 
			/// \param uiIntervall --> the intervall
			/// 
			/// \return ---
			// ------------------------------------------------------
			void Start(const unsigned &uiInterval = 1000);


			// ------------------------------------------------------
			/// void Stop(void)
			///
			/// Stop the periodic call of the function Notify
			/// 
			/// 
			/// \param uiIntervall --> the intervall
			/// 
			/// \return ---
			// ------------------------------------------------------
			void Stop(void);
	
	private:


			// ------------------------------------------------------
			/// void *Entry(void)
			///
			/// thread execution starts here
			/// 
			/// 
			/// \param ---
			/// 
			/// \return *void
			// ------------------------------------------------------
    		virtual void *Entry();


			// ------------------------------------------------------
			/// virtual void OnExit(void)
			///
			/// called when the thread exits - whether it terminates normally or is
			/// stopped with Delete() (but not when it is Kill()ed!)
			/// 
			/// 
			/// \param ---
			/// 
			/// \return *void
			// ------------------------------------------------------
    		virtual void OnExit(void);
			

			// -------------
			// private vars:
			// -------------

			///\brief the refresh interval
			unsigned m_uiInterval;
			
			///\brief a flag, that shows the thread is running, or not
			bool m_bRunning;
};
#endif //__wxThreadTimer_class__
// *************************************************************************
// * END OF FILE (CVS Concurrent Version Control)
// * -----------------------------------------------------------------------
// * $RCSfile: $
// * $Revision: $
// *************************************************************************


wxThreadTimer.cpp:

Code: Select all

#include "Application.h"

// declare the MyApp-Object in order to get access via wxGetApp().m_critsect
DECLARE_APP(MyApp)  

#include "wxThreadTimer.h"


wxThreadTimer::wxThreadTimer()
			  : wxThread()
			  , m_uiInterval(500)
			  , m_bRunning(false)
			  
{
	// ------------------
	// create a thread  :
	// -----------------
	if ( this->Create() != wxTHREAD_NO_ERROR )
	{
		wxLogError(wxT("wxThreadTimer::CreateThread::Can't create thread!"));
	}

	// ---------------------
	// register the thread :
	// ---------------------
   	wxCriticalSectionLocker enter(wxGetApp().m_critsect);
    wxGetApp().m_threads.Add(this);
}


wxThreadTimer::~wxThreadTimer()
{
	OnExit();
}



void wxThreadTimer::Start(const unsigned &uiInterval )
{
	m_uiInterval = uiInterval;
	m_bRunning   = true;
	if ( this->Run() != wxTHREAD_NO_ERROR )
	{
		wxLogError(wxT("wxThreadTimer::Start::Can't start thread!"));
	}
}

void wxThreadTimer::Stop()
{
	m_bRunning = false;
}

void wxThreadTimer::OnExit()
{
    wxCriticalSectionLocker locker(wxGetApp().m_critsect);

    wxArrayThread& threads = wxGetApp().m_threads;
    threads.Remove(this);

    if ( threads.IsEmpty() )
    {
        // signal the main thread that there are no more threads left if it is
        // waiting for us
        if ( wxGetApp().m_waitingUntilAllDone )
        {
            wxGetApp().m_waitingUntilAllDone = false;

            wxGetApp().m_semAllDone.Post();
        }
    }
}
void *wxThreadTimer::Entry()
{

    while(m_bRunning)
    {

        // check if we were asked to exit
        if ( TestDestroy() )
            break;

    		wxMutexGuiEnter();
				Notify();
  			wxMutexGuiLeave();
        
		wxThread::Sleep(m_uiInterval);
    }

    return NULL;
}

I've also added an working (with a linux makefile) example which demonstrates the use of the wxThreadTimer class.

Regards

Orbitcowboy
Attachments
wxThreadSample190208.tar.gz
(494.39 KiB) Downloaded 423 times
orbitcowboy
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Mon Jul 23, 2007 9:01 am

Post by orbitcowboy »

Here a picture of the sample programm
Attachments
sample.png
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

Some Feedback:

Nice Idea, but I would prefer to not have to derive from your class to actually use it.
So, you could create some interface, to use for communication.
I think creating your own Event could solve this, so that a class which likes to receive the notify, only has to implement an Event Handler.

phlox
User avatar
T-Rex
Moderator
Moderator
Posts: 1249
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Post by T-Rex »

Also it is not very good idea to make the class depend on wxGetApp(). You could use static wxCriticalSection member of your class instead of retrieving critical section object from wxGetApp().
orbitcowboy
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Mon Jul 23, 2007 9:01 am

Post by orbitcowboy »

Thanks for your comments,

i thought it is the correct way, to have a dependeny on the wxApp class because in the wxThreadSample it is implemented in the same way :? . I will reconsider my implementation again.

Regards

Orbitcowboy
Post Reply