• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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
0
chadmanvb
Asked:
chadmanvb
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
JimBrandleyCommented:
In the on-click event handler,
yourTimer.Stop();
yourOtherDialog.ShopDialog();
yourTimer.Enabled = true;

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

Jim
0
 
chadmanvbAuthor Commented:
I tried this already, but it seems to start the timer over every time and not start where it left off.  Chad
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
JimBrandleyCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
AutoeformsCommented:
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

greg
0
 
UnifiedISCommented:
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.
0
 
chadmanvbAuthor Commented:
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
0
 
AutoeformsCommented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is an example of what I described:

Public Class Form1

    Private targetDateTime As DateTime
    Private minutesInInterval As Integer = 5
    Private timeRemaining As TimeSpan

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 100
        SetNextTarget()
        Timer1.Start()
    End Sub

    Private Sub SetNextTarget(Optional ByVal resumeInterval As Boolean = False)
        If Not resumeInterval Then
            targetDateTime = DateTime.Now.AddMinutes(minutesInInterval)
        Else
            targetDateTime = DateTime.Now.Add(timeRemaining)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Stop()
        timeRemaining = targetDateTime.Subtract(DateTime.Now)

        ' do something in here...

        SetNextTarget(True)
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If DateTime.Now.Subtract(targetDateTime).TotalSeconds > 0 Then
            ' do something in here...

            SetNextTarget()
        End If
    End Sub

End Class
0
 
chadmanvbAuthor Commented:
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....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now