Results 1 to 22 of 22

Thread: Too fast for VB to detect? Stopwatch.

Threaded View

  1. #8
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Too fast for VB to detect? Stopwatch.

    To test this theory I wrote this code:

    Code:
       Dim stwTimer As Stopwatch = Stopwatch.StartNew()
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            stwTimer.Start()
            While stwTimer.ElapsedMilliseconds < 2000
                TextBox1.AppendText(stwTimer.ElapsedMilliseconds.ToString & vbCrLf)
            End While
            stwTimer.Stop()
        End Sub
    What it does it starts the stopwatch and appends the elapsedmilliseconds to a multiline textbox until 2 seconds have passed. Even though it is a really small loop I noticed 2 very important things. The first data written is 1913, which means the 2 seconds are almost up before the "while" completes the first loop! the second thing is that some numbers repeat, sometimes more than twice (like the 1914) but then some are skipped (1927) which tell me that performance of the code decreases while the textbox is being filled and in that case is not reliable to try to catch a specific milisecond.

    Here's a partial output of the code:

    1913
    1914
    1914
    1914
    1915
    1915
    1916
    1916
    1917
    1917
    1918
    1919
    1920
    1921
    1922
    1923
    1924
    1925
    1926
    1928
    1929
    1930
    Last edited by kaliman79912; Oct 17th, 2010 at 12:29 PM.

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