dcsimg
Results 1 to 14 of 14

Thread: Timer Test

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    11

    Timer Test

    I've programmed in Basic in the past but I'm brand new to VB. I'm trying to understand the timer function. I wrote a short program to display a count from 1 to 20 in one second intervals or so I thought.
    Here's the code:

    Dim Counter As Integer
    Counter = 1
    Do
    Timer1.Enabled = False
    TextBox1.Text = Counter
    Timer1.Enabled = True
    Counter = Counter + 1
    Loop While Counter < 21
    End

    I put this under a Button_Click event with the Timer_Tick listed first. The Timer_Tick has no code associated with it and the interval is 1000. When I run the progam, it stops in less than a second and ends and the form goes back to design mode.
    I've also tried For-Next and If statements to increment the counter but nothing works. What am I doing wrong?
    Thanks for the help.

  2. #2
    New Member b mitsai's Avatar
    Join Date
    Jul 2013
    Posts
    7

    Re: Timer Test

    You aren't. If timer don't have any code so what are you testing?
    Timer works this way:
    If timer is enabled, each milisecond (interval on the properties), run the code that is on timer function

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
         '*your code here will run here
    End Sub
    You don't need to make that while, i don't know why you are trying to do.

    Just make this simple program:

    Code:
    Public Class Form1
        Dim counter As Integer = 0
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            counter += 1
            TextBox1.Text = counter
        End Sub
    End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    11

    Re: Timer Test

    Thanks for the help. Sorry I didn't fully explain my problem. What I'm trying to do is start a counter with a button click that displays 1 to 20 in one second intervals and then stops. The Loop While statement stops the counter at twenty. I haven't run your example program but it has no button click event or test to stop counting. I'll play with it to see if I can get it to work the way I want.

    Also, my program is an example of the way "Basic" would do it. The += operator is much simpler.
    Thanks again.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,213

    Re: Timer Test

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            counter = 0
            Timer1.Interval = 1000 'one sec. in ms.
            Timer1.Enabled = True
        End Sub
    
        Dim counter As Integer = 0
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If counter >= 20 Then
                Timer1.Enabled = False
            Else
                counter += 1
                TextBox1.Text = counter.ToString
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    New Member b mitsai's Avatar
    Join Date
    Jul 2013
    Posts
    7

    Re: Timer Test

    but thats pointless xD
    You can do that without timers etc.

    edit: OH WAIT I GOT IT XD, see the dbasnett code
    Last edited by b mitsai; Jul 5th, 2013 at 08:34 AM.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,213

    Re: Timer Test

    It should be pointed out that this method should not be used as timing. In other words the actual elapsed time will not be 20 seconds when the counter reaches 20. If we add a stopwatch to the code we can see that.

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            counter = 0
            Timer1.Interval = 1000 'one sec. in ms.
            stpw.Reset()
            stpw.Start()
            Timer1.Enabled = True
        End Sub
    
        Dim counter As Integer = 0
        Dim stpw As New Stopwatch
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If counter >= 20 Then
                stpw.Stop()
                Debug.WriteLine(stpw.Elapsed)
                Timer1.Enabled = False
            Else
                counter += 1
                TextBox1.Text = counter.ToString
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    11

    Re: Timer Test

    Thanks much dbasnett. More info for my programming journey. I was wondering if "counter" needed to be a string or an integer to display. I ran the program both ways and it did fine. Not sure why. Also, placing the Dim statement between Subs evidently works globally for both. I also found out that the listing sequence of events is important.
    My reason for learning VB is to write a program to monitor my AC usage via an Arduino. I want to feed the data to Excel and graph it. This timer exercise is part of that. I also need to explore the TimeString function. I have more learning ahead and a long way to go.
    Thanks again.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,213

    Re: Timer Test

    It is a good habit to add
    Option Strict On
    as the first line of your program, or make it the default setting in the IDE.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    11

    Re: Timer Test

    I'll do that. I didn't know the stpw command existed. It's not listed in "Visual Basic Step by Step" that I'm using. Is there a reference out there that lists all the commands and functions for VB and what they do? It would be helpful to have that info available.
    Thanks.

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,213

    Re: Timer Test

    The documentation is always a good reference.

    http://msdn.microsoft.com/en-us/library/ff361664.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    11

    Re: Timer Test

    So, I ran this a number of times and saw no difference in the result. I assume the Debug.WriteLine statement is supposed to display the difference between the timer count and the stopwatch count but the program always stops after 20 is reached and nothing else happens. Am I doing something wrong?

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,213

    Re: Timer Test

    It was to show that more than 20 seconds had elapsed. Some would think that if the timer fired every 1000 ms. 20 times that 20 seconds would have elapsed. Because of the way the timer is handled this is not true, which is what the test tried to show.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    11

    Re: Timer Test

    Thanks, I understood that and I think I know why the difference. However, I'm curious as to why the difference didn't display. If you don't want to go any farther with this, I understand. You have other things to do.
    Thanks again.

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,213

    Re: Timer Test

    Try changing this line

    Code:
    Debug.WriteLine(stpw.Elapsed)
    to

    Code:
    TextBox1.Text = stpw.Elapsed.ToString
    The actual elapsed time was showing in the debug output window of the IDE. It would surprise me if it was 20.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width