Public Class Form1 'déclaration d'un nouveau thread appelant la sub "MaSub" Dim t As New Threading.Thread(AddressOf MaSub) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'on demmare le thread t.Start() 'si on met ici MaSub (sans t.Start) , Form1 est bloquée le temps du traitement du calcul End Sub Private Sub MaSub() 'code du traitement bloquant (calcul bidon pour l'exemple) For x = 0 To 200000000 Dim i As Double = Math.Cos(120) Dim j As Double = Math.Cos(200) Dim f As Double = i + j Next MessageBox.Show("fin") End Sub End Class
Public Class Form1 'déclaration d'un nouveau thread appelant la sub "MaSub" Dim t As New Threading.Thread(AddressOf MaSub) 'déclaration d'un délégué chargé d'appeler une sub (avec un parametre ici "Texte") Private Delegate Sub DelegateRemplirTextBox(ByVal texte As String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'on demmare le thread t.Start() 'si on met ici MaSub (sans t.Start) , Form1 est bloquée le temps du traitement du calcul End Sub Private Sub MaSub() 'code du traitement bloquant (calcul bidon pour l'exemple) Dim resultat As Double For x = 0 To 200000000 Dim i As Double = Math.Cos(120) Dim j As Double = Math.Cos(200) resultat = i + j 'pour éviter les opérations inter-threads on est obligé 'de faire appel à un délégué qui va se charger d'appeler lui même 'la sub RemplirTextBox avec "resultat" en parametre (transformé en string pour le textbox) If Me.InvokeRequired = True Then Me.Invoke(New DelegateRemplirTextBox(AddressOf RemplirTextBox), resultat.ToString) End If 'petite pause pour simuler l'arrivée des données du port série Threading.Thread.Sleep(100) Next MessageBox.Show("fin du traitement") End Sub Private Sub RemplirTextBox(ByVal Texte As String) TextBox1.Text &= Texte End Sub End Class
Public Class Form1 Public Delegate Sub _Affiche_ASCII(ByVal donnee As String) Private m_CommPort As New Rs232() Public Sub Affiche_ASCII(ByVal donnee As String) RichTextBox2.AppendText(donnee) End Sub Private Sub WriteMessage(ByVal message As String) RichTextBox1.Text += message + vbCrLf End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim nb_octet As Integer = SerialPort1.BytesToRead ' on récupère le nombre d'octet présent dans le tampon Dim trame(nb_octet - 1) As Byte ' Tableau acceuillant les données au format byte SerialPort1.Read(trame, 0, nb_octet) 'on lit le port Dim donnee As String = System.Text.Encoding.ASCII.GetString(trame) ' on récupère les données au format ASCII If Me.InvokeRequired = True Then Me.Invoke(New _Affiche_ASCII(AddressOf Affiche_ASCII), donnee) End If End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing End End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try ComboBox1.Text = SerialPort1.BaudRate ComboBox1.Enabled = False SerialPort1.Open() Catch ex As Exception MsgBox("Impossible d'ouvrir le port ") End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ' Try ComboBox1.Enabled = True If SerialPort1 IsNot Nothing Then SerialPort1.Close() SerialPort1.Dispose() RichTextBox2.Text = "" End If End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Class Form1 Public Delegate Sub _Affiche_ASCII(ByVal donnee As String) Private m_CommPort As New Rs232() Private Sub WriteMessage(ByVal message As String) RichTextBox2.Text &= message & vbCrLf End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim nb_octet As Integer = SerialPort1.BytesToRead ' on récupère le nombre d'octet présent dans le tampon Dim trame(nb_octet - 1) As Byte ' Tableau acceuillant les données au format byte SerialPort1.Read(trame, 0, nb_octet) 'on lit le port Dim donnee As String = System.Text.Encoding.ASCII.GetString(trame) ' on récupère les données au format ASCII Dim t As New Threading.Thread(AddressOf MaSub) t.Start(donnee) End Sub Private Sub MaSub(ByVal Texte As String) If Me.InvokeRequired = True Then Me.Invoke(New _Affiche_ASCII(AddressOf WriteMessage), donnee) Else WriteMessage(Texte) End If End Sub
Public Class Form1 Public Delegate Sub _Affiche_ASCII(ByVal donnee As String) Private Sub WriteMessage(ByVal message As String) RichTextBox2.Text += message + vbCrLf End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim sp As System.IO.Ports.SerialPort = DirectCast(sender, System.IO.Ports.SerialPort) Dim t As New Threading.Thread(AddressOf MaSub) t.Start(sp) End Sub Private Sub MaSub(ByVal MonPort As System.IO.Ports.SerialPort) Dim nb_octet As Integer = MonPort.BytesToRead ' on récupère le nombre d'octet présent dans le tampon Dim trame(nb_octet - 1) As Byte ' Tableau acceuillant les données au format byte MonPort.Read(trame, 0, nb_octet) 'on lit le port Dim donnee As String = System.Text.Encoding.ASCII.GetString(trame) ' on récupère les données au format ASCII If Me.InvokeRequired = True Then Me.Invoke(New _Affiche_ASCII(AddressOf WriteMessage), donnee) Else WriteMessage(donnee) End If End Sub End Class