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.

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.