171 views
in GUI Development by
Hi,

How to pause the timer & resume the timer?

e.g: total time - 5000ms. i just need to pause the timer after 3000ms. and then i am resuming the timer after some time. it should run for remaining time period (2000ms)

How to achieve this?

1 Answer

0 votes
by
Hello,

maybe it is the easiest, if you start the timer with 3000ms and restart it later with 2000ms. Would that help for your use-case?

Best regards,
Manfred.
by
Hi,

what can I do? if i don't know, in which time, timer is stopped (or) when can i restarts the timer?

just want to pause the timer based on some conditions & resume the timer from the time, where we paused.

How can i achieve this?
by

Hello Gayathri,

...resume the timer from the time, where we paused.

You can Start, stop and restart the Timer object. Pause and then resume the timer is less simpe, but possible.

Idea 1: You will need to remember the time when the timer was paused and when the timer is resumed calculate the remaining time and restart the timer with this value. To query the current time you can use the method GetCurrentTime(). Following could be the code to start, pause and resume the timer:

// ------- Start timer

// Remember the time when the timer is started in a variable (e.g. uint32 StartTime)
// and then start the timer.
StartTime     = Timer.GetCurrentTime();
Timer.Begin   = 0;
Timer.Enabled = true;

// -------- Pause timer

// Estimate how much time has passed since the timer was started and accumulate the
// value in a variable (e.g. int32 ElapsedTime). Then stop the timer.
ElapsedTime   += int32( Timer.GetCurrentTime() - StartTime );
Timer.Enabled  = false;

// -------- Resume timer

// Adjust the timer's first expiration time to a value corresponding the remaining
// time and start the timer again. 
StartTime     = Timer.GetCurrentTime();
Timer.Begin   = math_max( Timer.Period - ElapsedTime, 0 );
Timer.Enabled = true;

If the timer is intended to expire periodically, update the variable storing the start time (e.g. uint32 StartTime) with each expiration. For example:

// ----- Slot method invoked by the timer.

StartTime = Timer.GetCurrentTime();

Idea 2: Other, possibly much more easy idea is to configure the timer to run with higher frequency. For example, 100 ms (10 Hz). Each timer expiration increments  a counter and compares the counter with a desired threshold. To pause the counting just disable the timer. For example:

// ------- Start timer

// Reset the counter variable (e.g. int32 Counter) and start the timer
Counter       = 0;
Timer.Enabled = true;

// -------- Pause timer

// Just stop the timer.
Timer.Enabled = false;

// -------- Resume timer

// Just re-start the timer again.
Timer.Enabled = true;

// ----- Slot method invoked by the timer.

// In the slot method triggered by the timer, count the expirations
if ( ++Counter == 100 )
  trace "100 expiration รก 100 ms == 10 sec are elapsed.";

I hope it helps you further.

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...