Quantcast
Channel: VBForums - WPF, WCF, WF
Viewing all articles
Browse latest Browse all 256

Aborting a thread running an infinite loop

$
0
0
Good morning all,
I've come to a problem on a vb.net wpf code that i didn't developed and that I'm not very comfortable with the language.
Basically, i have a PLC-PC communication and i want to update the values on the form on a periodic way.
For that i have a main page, inside that main page i have a Frame, and when change pages i load the new page to the frame, maintaining the main page always running throw-out the program:

Code:

Private Sub buttonIOState_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonIOState.Click
        SetCursorPos(0, 0)
        Frame1.Content = New IOState
End Sub

When i press the button described on top ( i have 5 of this buttons/pages that the user can freely navigate through ), the page when loaded runs the following code:

Code:

Private Sub Page_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        updateUIThread.IsBackground = True
        updateUIThread.Start()
        running = True
End Sub

Delegate Sub updateUIDelegate()
Public updateUI2 As updateUIDelegate = AddressOf updateUIfunc

Private Sub updateUIfunc()
        textBlockReadPressureT1.Text = intVariables("readingPressureCaixao1").reading
        textBlockReadPressureT2.Text = intVariables("readingPressureCaixao2").reading

        textBlockReadX1.Text = Format(intVariables("readingX1").reading / 10, "0.0")
        textBlockReadX2.Text = Format(intVariables("readingX2").reading / 10, "0.0")
        textBlockReadX3.Text = Format(intVariables("readingX3").reading / 10, "0.0")
        textBlockReadX4.Text = Format(intVariables("readingX4").reading / 10, "0.0")

 End Sub

Private Sub updateUI()
    While running
            Try
                Me.Dispatcher.Invoke(updateUI2)
                Thread.Sleep(200)
             
            Catch ex As Exception
                MessageBox.Show("Error thread Manual updateUI:" & vbCrLf & ex.ToString)
                Exit While
            End Try
        End While
    End Sub


    Private Sub Page_Unloaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Unloaded
       
        running = False
        Try
            System.Threading.Thread.Sleep(50)
            updateUIThread.Abort()
        Catch ex As Exception
            MessageBox.Show("Manual State 2:" & vbCrLf & ex.ToString)
        End Try


    End Sub

Every time i load a new page into the frame i run the previous code, so that i can upload the values present on the screen in "real-time".
Problem:
- The program overtime is getting slower and slower.
- Most of the times that i change page i get a thread error
Name:  2.jpg
Views: 258
Size:  31.0 KB

I can't know when the thread has finished, because i only want the thread to finish when the page is unloaded.
I've read about Background workers, but I'm not sure that is the correct path.
All help would be appreciated.
Thank you in advance for your time.
Attached Images
 

Viewing all articles
Browse latest Browse all 256

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>