Problem when obtaining value from a variable VB.net

0

veran I have a function that is responsible for moving a linear motor, for this I compare two values and if they are equal, the movement function is executed.

While contador1 <= total1

    MsgBox("")

    If slide1 = desde Then
        TrySetTarget(0, hasta)
        System.Threading.Thread.Sleep(1)
    End If

    If slide1 = hasta Then
        TrySetTarget(0, desde)
        System.Threading.Thread.Sleep(1)
        contador1 = contador1 + 1
        If TextBox15.Text <= 1 Then
            contador1 = contador1 + 2
            TextBox15.Text = TextBox15.Text - 1
        Else
            System.Threading.Thread.Sleep(1)
            TextBox15.Text = TextBox15.Text - 1

        End If
    End If




End While

The function above is responsible for moving the engine and this:

Sub TryGetVariables()

    Dim a As String
    Dim b As String
    Dim c As String


    Dim gf1 As String
    Dim gt1 As String
    Try
        Using device As Usc = connectToDevice() ' Find a device and temporarily connect.


            device.getVariables(st1)


            galfaf1 = st1(1).position
            galfat1 = st1(2).position


            slide1 = st1(0).position
            valor = Me.slide1

            a = (5 * galfaf1) / 1023
            b = (5 * galfat1) / 1023


            c = 0.1 * (slide1 / 4 - 1000)



            If a >= 0 And a <= 0.1 Then
                gf1 = 19.23 * a
                'MsgBox(gf1)
                TextBox5.Text = gf1
            ElseIf a > 0.1 And a <= 1.3 Then
                gf1 = 19.23 * a + 25
                'MsgBox(gf1)
                TextBox5.Text = gf1
            ElseIf a > 1.3 And a <= 2.5 Then
                gf1 = 225 * (a - 1.3) + 50
                'MsgBox(gf1)
                TextBox5.Text = gf1
            ElseIf a > 2.5 And a <= 3.4 Then
                gf1 = 755.5 * (a - 2.5) + 320
                'MsgBox(gf1)
                TextBox5.Text = gf1
            End If

            If b >= 0 And b <= 0.1 Then
                gt1 = 19.23 * b
                TextBox12.Text = gt1
            ElseIf b > 0.1 And b <= 1.3 Then
                gt1 = 19.23 * b + 25
                TextBox12.Text = gt1
            ElseIf b > 1.3 And b <= 2.5 Then
                gt1 = 225 * (b - 1.3) + 50
                TextBox12.Text = gt1
            ElseIf b > 2.5 And b <= 3.4 Then
                gt1 = 755.5 * (b - 2.5) + 320
                TextBox12.Text = gt1
            End If

            TrackBar1.Value = c


            ' device.Dispose() is called automatically when the "Using" block ends,
            ' allowing other functions and processes to use the device.
        End Using
    Catch exception As Exception  ' Handle exceptions by displaying them to the user.
        displayException(exception)
    End Try
End Sub

The TryGetVariables () function is executed by means of a Timer every 50 milliseconds and slider1, the variable that gives me problems is refreshed in it, my problem is that when I use it in the code above it does not obtain value and only when I put an MsgBox, it gets value and the engine moves, I tried everything, placing the function within the first code chunk and not using the Timer but it does not work properly because it does not perform all the code's functionality. I do not know what to do, no matter how much I think about it, please appreciate your help.

    
asked by som1995 29.06.2017 в 13:31
source

1 answer

0

This is an assumption, since without seeing all the code it is not easy to analyze it.

The problem in your code is the loop you have: While contador1 <= total1 ...

What is happening to you is that this loop is monopolizing the whole flow of the program, not allowing the events to fire and therefore TryGetVariables is executed. And since the loop depends on the variables that are updated outside the loop, that's why it does not work correctly.

By putting a MsgBox or, as in my comment I told you, a Application.DoEvents() , what you do is stop the execution of the loop momentarily and allow all the events that were queued waiting to fire can do so.

The logical thing is that if you depend on the TryGetVariables method within that loop, execute it inside:

While contador1 <= total1

    TryGetVariables()

    If slide1 = desde Then
        TrySetTarget(0, hasta)
        System.Threading.Thread.Sleep(1)
    End If

   If slide1 = hasta Then
        TrySetTarget(0, desde)
        System.Threading.Thread.Sleep(1)
        contador1 = contador1 + 1
        If TextBox15.Text <= 1 Then
            contador1 = contador1 + 2
            TextBox15.Text = TextBox15.Text - 1
        Else
            System.Threading.Thread.Sleep(1)
            TextBox15.Text = TextBox15.Text - 1

        End If
    End If
End While
    
answered by 29.06.2017 / 14:02
source