Link to home
Start Free TrialLog in
Avatar of chadmanvb
chadmanvb

asked on

Pause a timer .net

I have an application that needs to pause a timer if a user hits a button.  When the user hits the button it opens another form and can take 60 seconds to run.  After the button press completes its job I would like the timer to resume where it left off.  Is there an easy way to do this?  Chad
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

In the on-click event handler,
yourTimer.Stop();
yourOtherDialog.ShopDialog();
yourTimer.Enabled = true;

Jim
Instead of ShopDialog(), make that ShowDialog(), and for VB, remove the semicolons.

Jim
Avatar of chadmanvb
chadmanvb

ASKER

I tried this already, but it seems to start the timer over every time and not start where it left off.  Chad
It has no pause method. You could do this:
1. Get the current time from the system when you start the timer.
2. Get the current time when the button is pressed and save the difference from the start time.
3. Run the dialog.
4. Start the timer with a new interval = original interval - difference.
5. When timer next fires, reset the interval to the original interval.

Jim
Avatar of Mike Tomlinson
If you want it to "resume" with only the portion of the interval that was "left" then you need to calculate an actual datetime that the next "event" should fire.  Then instead of having an Interval of the actual time period you want, you instead use a shorter interval and poll the current time to see if your event should fire.

When you "pause", you calculate how much time is left by subtracting the current time from the target time.  When you resume, your new target time is simply the current time plus the "remainder" from the previous operation.  Use the TimeSpan class to store the remainder.
SOLUTION
Avatar of Autoeforms
Autoeforms

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
an addendum to JimBrandley's 4 & 5:
set the interval before enabling/starting the timer or else it won't take effect until after the first elapsed event.

Autoeforms solution will keep the timer running but it won't pause the timer so the amount of time that will pass until the next elapsed event will be variable.
Could you explain this?  I am not sure how to set this up:

take an easier approach.
create a private private p_bSkipTimerCode as boolean = false

as the first line in your timer code
if me.p_bSkipTimerCode then exit sub.

then in your fire logic

me.p_bSkipTimerCode = true
yourOtherDialog.ShopDialog()
me.p_bSkipTimerCode = false

this should do it quick and simple


Chad
my understanding is you want the time to continue to run on the same interval. stopping and restarting resets the timer.

this method creates a private variable in your form where the timer is created or a global if it is a system wide for you.

in your timer code you check to see if ithe value is set to true and exit first thing not doing any work. in effect you are allowing the timer to continue running but short circuting it from doing anything.

set the value to true before firing your form this creates the short circuit. reset it to false when back from your form. this enables the circuit.

does this help
g

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks idle, that worked great.

auto, I think your idea would have also worked, but I could not figure out how to set it up.  Sorry, but I am not very good at vb yet....