StopWatch Loop using VB.NET

Novemberland picture Novemberland · May 11, 2013 · Viewed 12.6k times · Source

I want to create a simple timer with this interface using VB.NET

enter image description here

I want to press Button1 and to start counting seconds in the textbox.

I do not want to use the Timer Component because it does not offer high resolution.

So, I decided to use a stopWatch Class due to its high resolution according to specifications.

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.

Is there anything wrong with my code? What should I do to have the functionality described above?

Thanks in advance!

 
Public Class Form1

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

End Class

Answer

Andreas picture Andreas · May 11, 2013

In WinForms, there is one UI thread executing the message loop. Basically, every event is added to the message queue, which is processed, one event after another. When Button1 is clicked, the Button1_Click method is executed and no other event will be processed until it finishes. Since your design requires Button2.Click to be processed in order to terminate the loop in Button1.Click, it will never terminate.

To correctly implement what you want, you'd have to start the stopwatch on Button1.Click and put the UI update logic into the Tick event of a timer which you place on the form.