Home All Groups Group Topic Archive Search About

Calculate elapsed time

Author
6 Feb 2006 8:53 PM
techspirit
Hi,

I am seeking help for calculating the elapsed time on an activity.
When the user clicks "pause" button on a form, the timer should also
pause. When the user clicks "continue", the timer should continue from
where the user paused.
For example, if the user paused after 10 seconds of progress in the
activity, it should start from the 11th second when the user clicks
"continue".
Am using .Net version 1.1, so if stopwatch performs a similar action,
it can be ruled out.
Appreciate any help on this using vb.net or directions to a
discussion/link/articles with respect to this query.
Thanks
TS

Author
6 Feb 2006 11:27 PM
Justin Swan (MSFT)
Hi techspirit, I think there's a pretty easy way to accomplish what you're
looking for assuming I understand your situation.  I created a small winapp
to show how to do this, it contains 2 buttons, a timer, and a text box.  One
Button starts the timer, the other pauses it, and the start button will
resume the timer where it left off.  The textbox merely displays the time. 
This seems to do what you are looking for.  Below I've included the code from
form1.vb:


Public Class Form1

    Dim m_seconds As Integer

    Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles StartButton.Click
        Me.Timer1.Start()
        Me.Timer1.Enabled = True
    End Sub

    Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PauseButton.Click
        Me.Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
        m_seconds = m_seconds + 1
        Me.TextBox1.Text = m_seconds.ToString()
    End Sub
End Class

Hope this helps!


Show quoteHide quote
"techspirit" wrote:

> Hi,
>
> I am seeking help for calculating the elapsed time on an activity.
> When the user clicks "pause" button on a form, the timer should also
> pause. When the user clicks "continue", the timer should continue from
> where the user paused.
> For example, if the user paused after 10 seconds of progress in the
> activity, it should start from the 11th second when the user clicks
> "continue".
> Am using .Net version 1.1, so if stopwatch performs a similar action,
> it can be ruled out.
> Appreciate any help on this using vb.net or directions to a
> discussion/link/articles with respect to this query.
> Thanks
> TS
>
>
Author
7 Feb 2006 4:16 AM
techspirit
Hi Justin,
Thanks for replying. Your code extract sure works. But, my question is
can I acccomplish the same with a time based structure.I will need to
display the counter in seconds.

Thanks
TS

Justin Swan (MSFT) wrote:
Show quoteHide quote
> Hi techspirit, I think there's a pretty easy way to accomplish what you're
> looking for assuming I understand your situation.  I created a small winapp
> to show how to do this, it contains 2 buttons, a timer, and a text box.  One
> Button starts the timer, the other pauses it, and the start button will
> resume the timer where it left off.  The textbox merely displays the time.
> This seems to do what you are looking for.  Below I've included the code from
> form1.vb:
>
>
> Public Class Form1
>
>     Dim m_seconds As Integer
>
>     Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles StartButton.Click
>         Me.Timer1.Start()
>         Me.Timer1.Enabled = True
>     End Sub
>
>     Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles PauseButton.Click
>         Me.Timer1.Enabled = False
>     End Sub
>
>     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Timer1.Tick
>         m_seconds = m_seconds + 1
>         Me.TextBox1.Text = m_seconds.ToString()
>     End Sub
> End Class
>
> Hope this helps!
>
>
> "techspirit" wrote:
>
> > Hi,
> >
> > I am seeking help for calculating the elapsed time on an activity.
> > When the user clicks "pause" button on a form, the timer should also
> > pause. When the user clicks "continue", the timer should continue from
> > where the user paused.
> > For example, if the user paused after 10 seconds of progress in the
> > activity, it should start from the 11th second when the user clicks
> > "continue".
> > Am using .Net version 1.1, so if stopwatch performs a similar action,
> > it can be ruled out.
> > Appreciate any help on this using vb.net or directions to a
> > discussion/link/articles with respect to this query.
> > Thanks
> > TS
> >
> >
Author
7 Feb 2006 4:16 AM
techspirit
Hi Justin,
Thanks for replying. Your code extract sure works perfect. But, my
question is can I acccomplish the same with a time based structure.I
will need to display the counter in seconds.

Thanks
TS

Justin Swan (MSFT) wrote:
Show quoteHide quote
> Hi techspirit, I think there's a pretty easy way to accomplish what you're
> looking for assuming I understand your situation.  I created a small winapp
> to show how to do this, it contains 2 buttons, a timer, and a text box.  One
> Button starts the timer, the other pauses it, and the start button will
> resume the timer where it left off.  The textbox merely displays the time.
> This seems to do what you are looking for.  Below I've included the code from
> form1.vb:
>
>
> Public Class Form1
>
>     Dim m_seconds As Integer
>
>     Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles StartButton.Click
>         Me.Timer1.Start()
>         Me.Timer1.Enabled = True
>     End Sub
>
>     Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles PauseButton.Click
>         Me.Timer1.Enabled = False
>     End Sub
>
>     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Timer1.Tick
>         m_seconds = m_seconds + 1
>         Me.TextBox1.Text = m_seconds.ToString()
>     End Sub
> End Class
>
> Hope this helps!
>
>
> "techspirit" wrote:
>
> > Hi,
> >
> > I am seeking help for calculating the elapsed time on an activity.
> > When the user clicks "pause" button on a form, the timer should also
> > pause. When the user clicks "continue", the timer should continue from
> > where the user paused.
> > For example, if the user paused after 10 seconds of progress in the
> > activity, it should start from the 11th second when the user clicks
> > "continue".
> > Am using .Net version 1.1, so if stopwatch performs a similar action,
> > it can be ruled out.
> > Appreciate any help on this using vb.net or directions to a
> > discussion/link/articles with respect to this query.
> > Thanks
> > TS
> >
> >
Author
7 Feb 2006 4:42 AM
techspirit
Hi,

Your code was a good place to start working with the counter display.
I used this code , referring to a discussion in this group, to convert
the counter to seconds.
  Dim seconds As Integer = 75
    Dim elapsed As TimeSpan = TimeSpan.FromSeconds(seconds)
    Dim value As DateTime = DateTime.MinValue.Add(elapsed)
    Dim s As String = value.ToString("mm:ss")

Thanks, Justin.
Author
11 Feb 2006 4:32 AM
techspirit
techspirit wrote:
> Hi,
>
> Your code was a good place to start working with the counter display.
> I used this code , referring to a discussion in this group, to convert
> the counter to seconds.
>   Dim seconds As Integer = 75
>     Dim elapsed As TimeSpan = TimeSpan.FromSeconds(seconds)
>     Dim value As DateTime = DateTime.MinValue.Add(elapsed)
>     Dim s As String = value.ToString("mm:ss")
>
> Thanks, Justin.


Ok, I didnt really need all those lines of code. It was much easier to
use :
((New Date).AddSeconds(ElapsedTime).ToString("HH:mm:ss"))