Results 1 to 4 of 4

Thread: What is wrong with my timer or the code relating to my timer?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Question What is wrong with my timer or the code relating to my timer?

    I am going through Project Euler and I was interested to know what the time difference would be between running it on my netbook and somebody running it on a decent computer so I tried putting a timer in.

    Every time I try it, the message box says that it was done in 0 seconds, but if I time it on a stopwatch I know it takes about 36 seconds.
    The timer interval is 100 which is 0.1 seconds, right?

    Here's my code

    Code:
    Public Class Form1
        Dim time As Decimal = 0
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim found As Boolean = False
            Dim i As Integer = 1
            Dim ii As Integer = 1
    
            Timer1.Start()
    
            Do Until found = True
                If i Mod ii = 0 Then
                    If ii = 20 Then
                        found = True
                    Else : ii = ii + 1
    
                    End If
                Else : i = i + 1
                    ii = 1
                End If
            Loop
    
            Timer1.Stop()
    
            MsgBox("The number " & i & " was found in " & time & " seconds")
    
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            time = time + 0.1
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: What is wrong with my timer or the code relating to my timer?

    That's not what a Timer is for. If you want to measure how much time has elapsed then you use a Stopwatch. A Timer is to tell you when a specific amount of time has passed and to allow you to do something when that time has elapsed. If you want to do something and know how long it took, that's what a Stopwatch is for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: What is wrong with my timer or the code relating to my timer?

    And to explain why it's showing 0 rather than 36 is because of that old chestnut of processing items on the UI thread. Events aren't magical creatures from the operating system calling your code, the framework supplies the event abstraction over the top of the windows message queue. When a UI event happens (such as a button being clicked or a timer ticking), what happens is that a message is written to the windows message queue. The heart of a Windows Forms application (in fact, any windows program) is a loop that is constantly checking for messages in the queue, and when there are any, it reacts to them and then resumes checking. What does reacting to the messages mean? At a very hand-wavy level, it means figuring out if you have registered an event handler for that event, and if so executing the event handler code. Putting those last two bits together, you can see that all of an event handler must run before any more events are checked for.

    Therefore, to get back to your code, when you click the button, the button click handler is run, which as part of its processing enables the timer which starts ticking. HOWEVER, that just writes the tick event to the windows message queue, nothing more. 36 seconds (and 360 ticks of the timer later), the timer is stopped and the button click event handler exits, but not before writing the current value of time (which since it hasn't been changed in the button click handler [which is the only code we've executed] is 0).

    The loop gets back to processing the message queue, which is now filled with 360 tick messages. The tick handler is executed 360 times, with the time variable ending up holding 36.


    I'm not going to show you how to get around this behaviour and make your approach work, because jmc has already shown you the correct approach for your scenario. This is just to explain why your approach doesn't work.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: What is wrong with my timer or the code relating to my timer?

    Thank you, I did not realise there was a stopwatch.
    I have got it working now

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
  •  



Click Here to Expand Forum to Full Width