documentation for details and revise your code accordingly: this change was
unfortunately needed as the old class didn't behave correctly in all cases
+- small change to wxStopWatch::Pause() semantics, please see the documentation
+
All (GUI):
- the event type constants are not constants any more but are dynamically
\section{\class{wxStopWatch}}\label{wxstopwatch}
-The wxStopWatch class allow you to measure time intervals.
+The wxStopWatch class allow you to measure time intervals. For example, you may
+use it to measure the time elapsed by some function:
+
+\begin{verbatim}
+ wxStopWatch sw;
+ CallLongRunningFunction();
+ wxLogMessage("The long running function took %ldms to execute",
+ sw.Time());
+ sw.Pause();
+ ... stopwatch is stopped now ...
+ sw.Resume();
+ CallLongRunningFunction();
+ wxLogMessage("And calling it twice took $ldms in all", sw.Time());
+\end{verbatim}
\wxheading{Include files}
Pauses the stop watch. Call \helpref{wxStopWatch::Resume}{wxstopwatchresume} to resume
time measuring again.
-\membersection{wxStopWatch::Start}
-
-\func{void}{Start}{\param{long}{ milliseconds = 0}}
-
-(Re)starts the stop watch with a given initial value.
+If this method is called several times, {\tt Resume()} must be called the same
+number of times to really resume the stop watch. You may, however, call
+\helpref{Start}{wxstopwatchstart} to resume it unconditionally.
\membersection{wxStopWatch::Resume}\label{wxstopwatchresume}
\func{void}{Resume}{\void}
-Resumes the stop watch after having been paused with \helpref{wxStopWatch::Pause}{wxstopwatchpause}.
+Resumes the stop watch which had been paused with
+\helpref{wxStopWatch::Pause}{wxstopwatchpause}.
+
+\membersection{wxStopWatch::Start}\label{wxstopwatchstart}
+
+\func{void}{Start}{\param{long}{ milliseconds = 0}}
+
+(Re)starts the stop watch with a given initial value.
\membersection{wxStopWatch::Time}
{
public:
// ctor starts the stop watch
- wxStopWatch() { Start(); }
- void Start(long t = 0);
- void Pause() { m_pause = GetElapsedTime(); }
- void Resume() { Start(m_pause); }
+ wxStopWatch() { m_pauseCount = 0; Start(); }
- // get elapsed time since the last Start() or Pause() in milliseconds
+ // start the stop watch at the moment t0
+ void Start(long t0 = 0);
+
+ // pause the stop watch
+ void Pause() { if ( !m_pauseCount++) m_pause = GetElapsedTime(); }
+
+ // resume it
+ void Resume() { if ( !--m_pauseCount ) Start(m_pause); }
+
+ // get elapsed time since the last Start() in milliseconds
long Time() const;
protected:
long GetElapsedTime() const;
private:
- wxLongLong m_t0; // the time of the last Start()
- long m_pause; // the time of the last Pause() or 0
+ // the time of the last Start()
+ wxLongLong m_t0;
+
+ // the time of the last Pause() (only valid if m_pauseCount > 0)
+ long m_pause;
+
+ // if > 0, the stop watch is paused, otherwise it is running
+ int m_pauseCount;
};
#endif // wxUSE_STOPWATCH
#undef TEST_ALL
static const bool TEST_ALL = TRUE;
#else
- #define TEST_FILENAME
+ #define TEST_TIMER
static const bool TEST_ALL = FALSE;
#endif
puts("*** Testing wxStopWatch ***\n");
wxStopWatch sw;
- printf("Sleeping 3 seconds...");
+ sw.Pause();
+ printf("Initially paused, after 2 seconds time is...");
+ fflush(stdout);
+ wxSleep(2);
+ printf("\t%ldms\n", sw.Time());
+
+ printf("Resuming stopwatch and sleeping 3 seconds...");
+ fflush(stdout);
+ sw.Resume();
wxSleep(3);
printf("\telapsed time: %ldms\n", sw.Time());
sw.Pause();
- printf("Sleeping 2 more seconds...");
+ printf("Pausing agan and sleeping 2 more seconds...");
+ fflush(stdout);
wxSleep(2);
printf("\telapsed time: %ldms\n", sw.Time());
sw.Resume();
- printf("And 3 more seconds...");
- wxSleep(3);
+ printf("Finally resuming and sleeping 2 more seconds...");
+ fflush(stdout);
+ wxSleep(2);
printf("\telapsed time: %ldms\n", sw.Time());
wxStopWatch sw2;
}
putchar('.');
+ fflush(stdout);
}
puts(", ok.");
long wxStopWatch::GetElapsedTime() const
{
- return (wxGetLocalTimeMillis() - m_t0).GetLo();
+ return (wxGetLocalTimeMillis() - m_t0).GetLo();
}
long wxStopWatch::Time() const
{
- return (m_pause ? m_pause : GetElapsedTime());
+ return m_pauseCount ? m_pause : GetElapsedTime();
}
#endif // wxUSE_LONGLONG