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: $
// *************************************************************************
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