From: Vadim Zeitlin Date: Tue, 9 Apr 2002 10:35:35 +0000 (+0000) Subject: fixed a bug in wxStopWatch::Pause() (wouldn't pause if called immediately after Start... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/677eff077ec0e0fbdda61f4e5e7c186e21c4fc47 fixed a bug in wxStopWatch::Pause() (wouldn't pause if called immediately after Start()) and changed Pause()/Resume() calls to nest git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15051 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 91dc43d63d..8ae00ccaa9 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -26,6 +26,8 @@ wxBase: 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 diff --git a/docs/latex/wx/stopwtch.tex b/docs/latex/wx/stopwtch.tex index cff119d2ca..148993136e 100644 --- a/docs/latex/wx/stopwtch.tex +++ b/docs/latex/wx/stopwtch.tex @@ -1,6 +1,19 @@ \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} @@ -25,17 +38,22 @@ Constructor. This starts the stop watch. 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} diff --git a/include/wx/timer.h b/include/wx/timer.h index 8cf9cbd482..7fff1bbdc3 100644 --- a/include/wx/timer.h +++ b/include/wx/timer.h @@ -191,12 +191,18 @@ class WXDLLEXPORT wxStopWatch { 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: @@ -204,8 +210,14 @@ 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 diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 7a9e0bb1af..9061e86d93 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -91,7 +91,7 @@ #undef TEST_ALL static const bool TEST_ALL = TRUE; #else - #define TEST_FILENAME + #define TEST_TIMER static const bool TEST_ALL = FALSE; #endif @@ -3032,18 +3032,28 @@ static void TestStopWatch() 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; @@ -3061,6 +3071,7 @@ static void TestStopWatch() } putchar('.'); + fflush(stdout); } puts(", ok."); diff --git a/src/common/timercmn.cpp b/src/common/timercmn.cpp index b9f1269ec0..1d89b6507d 100644 --- a/src/common/timercmn.cpp +++ b/src/common/timercmn.cpp @@ -161,12 +161,12 @@ void wxStopWatch::Start(long t) 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