Note that there are some explanatory texts on larger screens.

plurals
  1. POStopWatch Loop using VB.NET
    primarykey
    data
    text
    <p>I want to create a simple timer with this interface using VB.NET</p> <p><img src="https://i.stack.imgur.com/p08Sv.png" alt="enter image description here"></p> <p>I want to press Button1 and to start counting seconds in the textbox.</p> <p><a href="https://stackoverflow.com/questions/10470276/my-timer-in-vb-net-is-losing-one-second">I do not want to use the Timer Component because it does not offer high resolution.</a></p> <p>So, I decided to use a stopWatch Class due to its high resolution according to specifications.</p> <p>But according to my VB.NET code below it seems to me that the whole "dotnet adventure" is impossible. That is because when I press Button1 the whole form it freezes and I cannot press Button2 to stop the timer.</p> <p>Is there anything wrong with my code? What should I do to have the functionality described above?</p> <p>Thanks in advance!</p> <p><pre> Public Class Form1</p> <code>Private enableTime As TimeSpan Private stopWatch As New Stopwatch() Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click stopWatch.Start() If stopWatch.IsHighResolution Then Do If stopWatch.ElapsedTicks.Equals(TimeSpan.TicksPerSecond) Then enableTime = enableTime + TimeSpan.FromSeconds(1) TextBox1.Text = enableTime.ToString stopWatch.Restart() End If If Not stopWatch.IsRunning Then Exit Do End If Loop End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click stopWatch.Stop() stopWatch.Reset() End Sub </code></pre> <p>End Class</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload