IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Problème thread (débutant)


Sujet :

VB.NET

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 8
    Par défaut Problème thread (débutant)
    Bonjour, je récupère des données sur mon port com (évenementiel), à chaque réception de données, je modifie 6 label.text.
    J'ai essayé plusieurs solutions dont ceux cités sur le site de microsoft mais je ne trouve pas.

    Comment faire correctement l'appel de thread dans ce code et où le faire ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    Private Property thread1 As Thread
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ...
    ...
    ...
    End Sub
     
    Sub reception() Handles comPort.DataReceived
            Me.thread1 = New Thread(New ThreadStart(AddressOf Me.recu))
            Me.thread1.Start()
    End Sub
     
    Public Sub recu()
            Dim nBytes As Integer = comPort.BytesToRead()
            Dim comBuffer As Byte() = New Byte(nBytes - 1) {}
            Dim lu As Integer
            lu = comPort.Read(comBuffer, 0, nBytes)
            If lu = 46 Then
                 Select Case (Hex(comBuffer(3)))
                        Case "0"
                            Label1.Text = Int(comBuffer(39))
                            Label2.Text = Int(comBuffer(35))
                        Case "1"
                            Label3.Text = Int(comBuffer(39))
                            Label4.Text = Int(comBuffer(35))
                        Case "2"
                            Label5.Text = Int(comBuffer(39))
                            Label6.Text = Int(comBuffer(35))
                    End Select
            End If
    End Sub
    Ce code actuel me renvoi comme message
    Opération inter-threads non valide : le contrôle 'Label1' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé.
    Merci pour votre aide

  2. #2
    Membre chevronné Avatar de Jerede
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2010
    Messages
    271
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mai 2010
    Messages : 271
    Par défaut
    Tu ne peut modifier directement des élements de ta Form depuis un autre Thread. Dans ton cas, il faut utiliser monLabel.Invoke(Delegate)

  3. #3
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut control.invoke methode
    bonjour tuyn-txa
    voici comme tu utilise un controle control.invoke
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
     
     
     
    Imports System.Threading
    Public Class Form3
        Private thread1 As Thread
     
        'Delegue MAJ Label dans Form1
        Private Delegate Sub MajLabelDelegate(ByVal objHexCom As String, ByVal strLabel1 As String, ByVal strLabel2 As String)
        Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
        End Sub
        'sub qui sert à mettre à jour les labels
        Private Sub majLabel(ByVal strHexCom As String, ByVal strLabel1 As String, ByVal strLabel2 As String)
            Select Case strHexCom
                Case "0"
                    Label1.Text = strLabel1
                    Label2.Text = strLabel2
                Case "1"
                    Label3.Text = strLabel1
                    Label4.Text = strLabel2
                Case "2"
                    Label5.Text = strLabel1
                    Label6.Text = strLabel2
            End Select
     
        End Sub
        Sub reception() Handles comPort.DataReceived
            Me.thread1 = New Thread(New ThreadStart(AddressOf Me.recu))
            Me.thread1.Start()
        End Sub
     
        Public Sub recu()
            Dim nBytes As Integer = comPort.BytesToRead()
            Dim comBuffer As Byte() = New Byte(nBytes - 1) {}
            Dim lu As Integer
            lu = comPort.Read(comBuffer, 0, nBytes)
            Dim strHexCom As String = ""
            Dim str1 As String = ""
            Dim str2 As String = ""
            If lu = 46 Then
                strHexCom = (Hex(comBuffer(3)))
                Select Case (Hex(comBuffer(3)))
                    Case "0"
                        str1 = Int(comBuffer(39))
                        str2 = Int(comBuffer(35))
     
                    Case "1"
                        str1 = Int(comBuffer(39))
                        str2 = Int(comBuffer(35))
                    Case "2"
                        str1 = Int(comBuffer(39))
                        str2 = Int(comBuffer(35))
                End Select
            End If
            ' Use the Control.Invoke() method of the current form,
            ' which is on the same thread as the rest of the controls.
            Me.Invoke(New MajLabelDelegate(AddressOf Me.majLabel), _
            New Object() {strHexCom, str1, str2})
        End Sub
     
    End Class
    bon code....

  4. #4
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 88

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 8
    Par défaut ...
    Merci MABROUKI ça marche

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec un thread (débutant en thread)?
    Par Jayceblaster dans le forum C#
    Réponses: 10
    Dernier message: 17/01/2007, 11h01
  2. Problème de débutant dans une requête
    Par decour dans le forum Access
    Réponses: 7
    Dernier message: 14/10/2005, 14h17
  3. [C#][service windows] problème de débutant avec 1 timer
    Par Nycos62 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 14/10/2005, 11h22
  4. Réponses: 3
    Dernier message: 24/09/2005, 09h34
  5. [DB2] problèmes de débutant
    Par rémi_tounul dans le forum DB2
    Réponses: 4
    Dernier message: 21/04/2005, 17h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo