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
This commit is contained in:
Vadim Zeitlin
2002-04-09 10:35:35 +00:00
parent 7b12790064
commit 677eff077e
5 changed files with 64 additions and 21 deletions

View File

@@ -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

View File

@@ -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}

View File

@@ -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

View File

@@ -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.");

View File

@@ -166,7 +166,7 @@ long wxStopWatch::GetElapsedTime() const
long wxStopWatch::Time() const
{
return (m_pause ? m_pause : GetElapsedTime());
return m_pauseCount ? m_pause : GetElapsedTime();
}
#endif // wxUSE_LONGLONG