Stopwatch.Elapsed total elapsed time

S-Soft 666 Reputation points
2023-10-29T13:42:31.08+00:00

Hello

How to check if Stopwatch.Elapsed total elapsed time is below 1 minute?

Dim ELTS As TimeSpan = Stopwatch.Elapsed

If ELTS.Minutes < 1 Then...

Does not seem to work correctly, it is true if total time is exactly 1 hour since minutes is less than 1.

Confused :(

Developer technologies | VB
Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2023-10-29T16:39:37.6233333+00:00

    Hi

    Here is a simplified stand alone example, not completely checked, but should work OK. See if this helps.

    ' Form1 with Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim sw As New Stopwatch
      Dim switch As Boolean = True
      Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Label1.Text = "Start"
      End Sub
      Sub DoTimer()
        sw.Start()
    
        ' simulate running process loop
        Do
    
          Label1.Text = GetTime(sw.ElapsedMilliseconds)
          Application.DoEvents()
    
          ' dummy load
          Threading.Thread.Sleep(1000)
    
        Loop Until switch
    
      End Sub
      Function GetTime(ms As Long) As String
        Dim secs As Long = 1000
        Dim mins As Long = 60 * secs
        Dim hrs As Long = 60 * mins
    
        Dim h As Long = (ms \ hrs)
        Dim m As Long = (ms - (h * hrs)) \ mins
        Dim s As Long = (ms - (h * hrs) - (m * mins)) \ secs
    
        Dim rs As String = s.ToString("00s")
    
        If m > 0 Then rs = m.ToString("00m ") & s.ToString("00s")
    
        If h > 0 Then rs = h.ToString("00h ") & m.ToString("00m ") & s.ToString("00s")
    
        Return rs
      End Function
      Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
        ' NOTE: sw is kept running
        If switch Then
          switch = False
          DoTimer()
        Else
          switch = True
        End If
      End Sub
    End Class
    
    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. KOZ6.0 6,735 Reputation points
    2023-10-29T13:56:47.6166667+00:00

    Let's take a look at the implementation of TimeSpan. TimeSpan holds Ticks.

    https://referencesource.microsoft.com/#mscorlib/system/timespan.cs

    if (ELTS.Ticks < TimeSpan.TicksPerMinute)
    

    You can also write it like this.

    1 person found this answer helpful.

  2. Dewayne Basnett 1,381 Reputation points
    2023-10-30T13:12:49.0266667+00:00

    How about the obvious,

            Dim oneMinute As TimeSpan = TimeSpan.FromMinutes(1)
            Dim totTimeString As String
            If myStopwatch.Elapsed < oneMinute Then
                totTimeString = String.Format("Proc time: {0} (secs.ms)", myStopwatch.Elapsed.ToString("ss\.fff"))
            Else
                totTimeString = String.Format("Proc time: {0} (hrs:min:sec)", myStopwatch.Elapsed.ToString("hh\:mm\:ss"))
            End If
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.