none
Create stopwatch or timer to see run time RRS feed

  • Question

  • I would like to see the run time of my code in a lable or text box displayed. I have tried the following code which doesnt begin to run until the function in that button is complete. the code I have just displays the seconds and when it reached 60 it just keeps counting in seconds doesnt convert to mins and seonds how would I go about changing this so it does convert? this? How do I alter my code so it displays the live current run time of the code in that button.?

    Imports System.Data.SqlClient
    Public Class Form1
        Dim StopWatch As New Diagnostics.Stopwatch
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            Label3.Text = Val(Label3.Text) + 1
            If Label3.Text = 0 Then Timer1.Enabled = False
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
             Timer1.Enabled = True
    
    ''''''The code I am running'''''
        End Sub

    Friday, May 10, 2019 11:05 AM

All replies

  • Hello,

    see https://stackoverflow.com/questions/18841933/vb-net-real-time-time-elapsed-feature

    Public Class Form2
    
    Private _elapseTimerRunning As Boolean = False
    Private _elapseStartTime As DateTime
    
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        txtTime.Text = Now.ToString("h:mm:ss tt")
        If _elapseTimerRunning = True Then
            Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
            txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
        End If
    End Sub
    
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        _elapseStartTime = DateTime.Now
        _elapseTimerRunning = True
    End Sub
    
    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        _elapseTimerRunning = False
    End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 10, 2019 11:36 AM
    Moderator
  • Thankyou for your reply,

    I dont want a start stop button I want the timer to run when I press the button to run my code.

    Friday, May 10, 2019 11:38 AM
  • Thankyou for your reply,

    I dont want a start stop button I want the timer to run when I press the button to run my code.

    You job is to review the code I provided then figure out how to apply the code to your task. There is nothing saying you must follow the code posted to a T, instead the job of a programmer is to find code and if not exactly what they want but the parts when applied to their task in a proper fashion is what you need to understand.

    So this may or may not be what you want but gives you an idea that code can be changed and that you don't need to use code as fully presented.

    Private Sub SomeTimer_Tick(sender As Object, e As EventArgs) Handles SomeTimer.Tick
        txtTime.Text = Now.ToString("h:mm:ss tt")
        
        Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
        txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
        
    End Sub
    Bottom line is think for yourself and adapt.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 10, 2019 11:56 AM
    Moderator
  • You didn't show when the timer is turned off but something like this

        Private stpw As New Stopwatch
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Label1.Text = stpw.Elapsed.ToString("hh\:mm\:ss")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            stpw.Restart()
            Timer1.Enabled = True
    
            ''''''The code I am running'''''
            '''''' Somewhere else disable the timer???  '''''
    
        End Sub
    

    Before you start the timer restart the stopwatch.


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Friday, May 10, 2019 2:02 PM
  • Don't make it more difficult than as it is

    Public Class Form1
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Static ts As New TimeSpan
            ts = ts.Add(TimeSpan.FromSeconds(1))
            Label3.Text = ts.ToString("hh\:mm\:ss")
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Timer1.Enabled = True
        End Sub
    End Class
    Be aware that nobody can type a zero and enter  in 1 second and therefore you cannot stop it by putting 0 in that. If you try it, it will again be overwritten by this code.


    Success
    Cor

    Friday, May 10, 2019 3:56 PM
  • Don't make it more difficult than as it is

    Public Class Form1
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Static ts As New TimeSpan
            ts = ts.Add(TimeSpan.FromSeconds(1))
            Label3.Text = ts.ToString("hh\:mm\:ss")
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Timer1.Enabled = True
        End Sub
    End Class
    Be aware that nobody can type a zero and enter  in 1 second and therefore you cannot stop it by putting 0 in that. If you try it, it will again be overwritten by this code.


    Success
    Cor


    The OP had the code to start the timer in a button.  This led me to believe that they might run the code more than once.  If so the code you posted will give them the total of all runs.  Might be what they want, might not.

    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Friday, May 10, 2019 5:07 PM

  • The OP had the code to start the timer in a button.  This led me to believe that they might run the code more than once.  If so the code you posted will give them the total of all runs.  Might be what they want, might not.


    Yea but he/she also doesn't want a stop button. So I thought this is something in between.  The essential is using the timespan in my code example.  

    :-)


    Success
    Cor

    Friday, May 10, 2019 5:33 PM