codice:
Imports System.Threading
Imports MySql.Data.MySqlClient
Public Class Form1
Public ThreadEst As Thread
Dim lettura As String ' lettura da seriale
Dim temp As String ' lettura temp
Dim um As String ' lettura umidità
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=;")
Dim MyCommand As New MySqlCommand
Public Sub ChangeX(ByVal x As Integer)
con.Open()
MyCommand.Connection = con
MyCommand.CommandText = "UPDATE salotto SET temp = '" & temp & "', um = '" & um & "'"
MyCommand.ExecuteNonQuery()
con.Close()
End Sub
Public Sub Changeb(ByVal b As Integer)
temp = (Mid(lettura, 6, 2))
um = (Mid(lettura, 14, 2))
TextBox1.Text = temp
TextBox2.Text = um
End Sub
Public Sub cancella()
SerialPort1.Open()
lettura = SerialPort1.ReadLine
SerialPort1.Close()
End Sub
Delegate Sub ChangeY(ByVal x As Integer)
Dim FunzioneEsterna As ChangeY = AddressOf Me.ChangeX
Dim FunzioneEsterna2 As ChangeY = AddressOf Me.Changeb
Dim FunzioneEsterna3 As ChangeY = AddressOf Me.cancella
Public Sub MainThreadEst()
Do
Thread.Sleep(100)
Dim cancella As Integer = 1
MyBase.Invoke(FunzioneEsterna3, cancella)
Thread.Sleep(1000)
Dim changeb As Integer = 1
MyBase.Invoke(FunzioneEsterna2, changeb)
Thread.Sleep(1000)
Dim changex As Integer = 1
MyBase.Invoke(FunzioneEsterna, changex)
Loop
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim threadesterno = New System.Threading.Thread(AddressOf MainThreadEst)
threadesterno.Name = "TrdEsterno"
threadesterno.Start()
End Sub
End Class
Con questo codice leggo la stringa dalla seriale, sempre dalla stringa tolgo le parti che non m interessano e le carico sul database.
Funziona perfettamente, ma non capisco perchè l applicazione ogni 3 secondi fa un freeze!
Se ho usato i thread perchè accade questo?