I'm trying to use the timer (not a Timer object) in a Do... Until loop. I want the loop to put a single variable into a label. When I do this it works and puts the value into the label (lblOxygenReading):

If btnGetOxygen.Text = "Get Oxygen Reading" Then
            Select Case True
                Case rdoColorWhite.Checked
                    strCurrentColor = "White"
                Case rdoColorRed.Checked
                    strCurrentColor = "Red"
                Case rdoColorBlue.Checked
                    strCurrentColor = "Blue"
                Case rdoColorGreen.Checked
                    strCurrentColor = "Green"
                Case rdoNoLight.Checked
                    strCurrentColor = "Dark"
            End Select

            Randomize()
            sglOxygen = ((Rnd(1) * 0.2) + 0.9) * sglOxygen
            lblOxygenReading.Text = sglOxygen.ToString("#.##")
            btnGetOxygen.Text = "Start Timing"
            Exit Sub
        End If

But when I do the following (it's part of a code in a button click event), it doesn't put the sglOxygen value into the lblOxygenReading label:

If btnGetOxygen.Text = "Start Timing" Then

            If strCurrentColor = "White" Then sglAdd = 0.13
            If strCurrentColor = "Red" Then sglAdd = 0.058
            If strCurrentColor = "Blue" Then sglAdd = 0.033
            If strCurrentColor = "Green" Then sglAdd = 0.0
            If strCurrentColor = "Dark" Then sglAdd = -0.033

            Dim TimeNow As Date
            Dim TimeLater As Date
            Dim intWaitTime As Integer = 1

  
            btnGetOxygen.Text = "Stop"

            For l = 1 To 300
                intWaitTime = 1 'number of seconds each
                TimeNow = DateTime.Now
                	Do
                	TimeLater = DateTime.Now
                	lblOxygenReading.Text = sglOxygen.ToString("#.##")
                        'Loop Until DateDiff(DateInterval.Second, TimeNow, TimeLater) >= intWaitTime
                '****************
                'This section varies the size between 90% and 110%
                Randomize()
                sglAdd = ((Rnd(1) * 0.2) + 0.9) * sglAdd
                '****************
                sglOxygen = sglOxygen + sglAdd 'Adds to O2
                If sglOxygen < 0 Then sglOxygen = 0
            Next l


            Exit Sub

It goes through the Do loop and the For.. next loop but doesn't change the label. Visual Studio doesn't show any errors when I'm writing the code, but when I run it, it hangs somewhere.

Dani AI

Generated

— the symptom (label never updates and the UI "hangs") is the classic result of blocking the UI thread with a tight loop. was correct that forcing a redraw or pumping messages can make the label change, but that is a workaround. A more robust solution is to stop blocking the UI thread and perform the waiting or periodic work with a timer or an async pattern so the form can redraw and remain responsive.

A simple, reliable approach for WinForms: use a System.Windows.Forms.Timer and do the per-second update inside its Tick handler. Keep the Random instance and the running state at form scope, start the timer on Start, stop it on Stop, and update the label inside Tick. Example pattern:

Private WithEvents updateTimer As New System.Windows.Forms.Timer()
Private rnd As New Random()

' Set up and start:
updateTimer.Interval = 1000 ' 1 second
updateTimer.Start()

' Tick handler:
Private Sub updateTimer_Tick(sender As Object, e As EventArgs) Handles updateTimer.Tick
    Dim factor = (rnd.NextDouble() * 0.2) + 0.9
    sglAdd *= factor
    sglOxygen += sglAdd
    If sglOxygen < 0 Then sglOxygen = 0
    lblOxygenReading.Text = sglOxygen.ToString("#.##")
End Sub

If you prefer newer patterns, an async loop with Await Task.Delay keeps the UI responsive without a Timer:

Private Async Sub StartSequence()
    For i = 1 To 300
        ' update values...
        lblOxygenReading.Text = sglOxygen.ToString("#.##")
        Await Task.Delay(1000)
    Next
End Sub

Troubleshooting tips: avoid creating a new Random each tick (use a field-level Random), use Control.Invoke/BeginInvoke if updating controls from a background thread, and wire the Stop button to stop the timer/loop. Application.DoEvents or manual Refresh can fix immediate redraws but may introduce reentrancy bugs—use them only for quick experiments, not production logic.

hi,

What is the purpose of that timer (Do ... Until ) ?

Anyhow try this

Do
	TimeLater = DateTime.Now
	lblOxygenReading.Text = sglOxygen.ToString("#.##")
        'Force to redraw the text
        lblOxygenReading.Refresh ()
        'Use Application.DoEvents() to help the to respond the user events

Loop Until DateDiff(DateInterval.Second, TimeNow, TimeLater) >= intWaitTime

Note: i added lblOxygenReading.Refresh ()
and you can use Application.DoEvents() to respond the user events

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.