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 :

probleme avec backgroundworker


Sujet :

VB.NET

  1. #1
    Candidat au Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Avril 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2016
    Messages : 4
    Points : 3
    Points
    3
    Par défaut probleme avec backgroundworker
    bonjour,
    j'ai un projet à faire, la consultation de slode sur le compte des téléphones mobiles et j'ai besoin de votre aide SVP, quelqu un peut me proposer un exemple d'application pour simuler ce projet

    le code a bien marché sans backgrounworker mais lorsque ajout le bgworker aucune réponse
    ===================================================================
    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
    Private Sub solde() 
     
    strCommand = String.Format("AT+CUSD=1," & Chr(34) & "{0}" & Chr(34) & ",15", "*" & "710" & "#") 
     
    Dim strName As String = cbxDevices.Text 
    Dim iDeviceSpeed As Integer 
     
    If (Not Integer.TryParse(cbxDeviceSpeed.Text, iDeviceSpeed)) Then 
    End If 
    objGsm.Open(strName, "0000", iDeviceSpeed) 
    If (objGsm.LastError <> 0) Then 
    If (objGsm.LastError = 36103) Then 
    MessageBox.Show("Invalid Pin entered: SIM card can be blocked after a number of false attempts in a row.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End If 
    End If 
    If (objGsm.LastError = 0) Then 
    objGsm.SendCommand(strCommand) 
    End If 
    ' Reads the response from the GSM Modem 
    If (objGsm.LastError = 0) Then 
    strResponse = objGsm.ReadResponse(10000) 
    End If 
     
    If (objGsm.LastError = 0) Then 
    If (strResponse.Contains("OK")) Then ' Response should be OK 
    objGsm.SendCommand(String.Empty) 
    strResponse = objGsm.ReadResponse(10000) 
     
    If (objGsm.LastError <> 0) Then 
    UpdateResult(objGsm.LastError) 
    Return 
    End If 
     
     
    If (strResponse.Contains("+CUSD:")) Then 
     
    strFields = strResponse.Split(Char.Parse(Chr(34))) 
     
    If (strFields.Length > 1) Then 
    strResponse = strFields(0) 
    Else 
    strResponse = strFields(1) 
    End If 
    End If 
    End If 
    End If 
    TextBox5.Text = strResponse 
    UpdateResult(objGsm.LastError) 
    objGsm.Close() 
    End Sub 
     
    Private Sub BGW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork 
    solde() 
    End Sub 
     
    Private Sub BGW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted 
    MsgBox("Mission complete") 
    End Sub
    ========================================================================================
    merci de mes aidés amis

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

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

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 381
    Points : 41 582
    Points
    41 582
    Par défaut

    Déclenches-tu le backgroundworker à un moment?

  3. #3
    Expert confirmé
    Avatar de wallace1
    Homme Profil pro
    Administrateur systèmes
    Inscrit en
    Octobre 2008
    Messages
    1 966
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Administrateur systèmes
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 966
    Points : 4 005
    Points
    4 005
    Billets dans le blog
    7
    Par défaut
    Bonjour,

    Je rejoins camarade Médinoc concernant le lancement de ton BackgroundWorker, il faut le déclencher :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    If not BGW.isBusy Then BGW.RunWorkerAsync
    Ensuite il y a autre chose qui me chagrine dans le corps de ta méthode BGW_DoWork, ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TextBox5.Text = strResponse
    IL FAUT SAVOIR QUE TU NE PEUX PAS METTRE A JOUR LES CONTROLES DE TON PROGRAMME SITUE COTE UI (OPERATION INTER-THREAD NON AUTORISEE) !!! IL FAUT LE FAIRE DEPUIS L'EVENEMENT ProgressChanged DE TON BGW


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
      Private Sub BGW_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW.ProgressChanged
            TextBox5.Text = e.UserState.ToString
        End Sub
    Mais avant cela il faut penser à paramétrer la propriété .WorkerReportsProgress à True pour autoriser l'utilisation de l'event ReportProgress :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    BGW.WorkerReportsProgress = True
    enfin pour basculer du corps de la méthode BGW_DoWork à BGW_ProgressChanged (pour mettre à jour des contrôles situés sur l'interface de ton programme) on procède ainsi :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    '......
    '........
    strResponse = strFields(1) 
    End If 
    End If 
    End If 
    End If 
    'On passe la valeur de strResponse en argument 
    BGW.ReportProgress(0, strResponse)
    'TextBox5.Text = strResponse 
    UpdateResult(objGsm.LastError) 
    objGsm.Close() 
    '.....
    Et on affecte cette valeur à la propriété Text du contrôle TextBox5 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      Private Sub BGW_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW.ProgressChanged
             'la valeur contenue dans e.UserState corresponds à strResponse = 2ème argument dans la signature plus haut !
            TextBox5.Text = e.UserState.ToString
        End Sub
    PS : Ne pas oublier de gérer les erreurs à l'arrivée dans la méthode : BGW_RunWorkerCompleted

    A+

  4. #4
    Candidat au Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Avril 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2016
    Messages : 4
    Points : 3
    Points
    3
    Par défaut aucune réaction backgroundworker
    merci amis pour les réponses mi aucune résulta changer

    il ya deux réponse ma donne :
    1: il affiche le message de termine le travaille mes aucune résulta

    2:travaille mes sans background et Le programme fonctionne, mais ne peut pas faire quoi que ce soit jusqu'à ce que le premier ouvrage complet

    merci

  5. #5
    Expert confirmé
    Avatar de wallace1
    Homme Profil pro
    Administrateur systèmes
    Inscrit en
    Octobre 2008
    Messages
    1 966
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Administrateur systèmes
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 966
    Points : 4 005
    Points
    4 005
    Billets dans le blog
    7
    Par défaut
    Pas évident de diagnostiquer sans savoir ce que contient l'appel de cette méthode :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    UpdateResult(objGsm.LastError)
    De plus il serait souhaitable que tu nous montre le code avec les modifications que tu as apportés !

    A+

  6. #6
    Candidat au Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Avril 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2016
    Messages : 4
    Points : 3
    Points
    3
    Par défaut
    salut amis com ta vous

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Sub UpdateResult2(ByVal nResult As Integer)
            'djjizzi résulta
            baha2 = (String.Format("{0}: {1}", nResult, objGsm.GetErrorDescription(objGsm.LastError)))
            TextBox3.Text = Mid(baha2, 4, 8)
        End Sub
    La réponse est envoyée par téléphone mobile à l'ordinateur ou mon programme

    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    Private Sub BGW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
            If e.Cancelled = True Then
                MsgBox("Canceled!")
            ElseIf e.Error IsNot Nothing Then
                MsgBox("Error: " & e.Error.Message)
            Else
                MsgBox("Done!")
            End If
        End Sub
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork
            soldDJEZZY()
        End Sub
     
        Private Sub BGW_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGW.ProgressChanged
            TextBox5.Text = e.UserState.ToString
        End Sub
     
        'buton solde djezzy
        Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
     
            If Not BGW.IsBusy Then BGW.RunWorkerAsync()
     
            strCommand = String.Format("AT+CUSD=1," & Chr(34) & "{0}" & Chr(34) & ",15", "*" & parrametre.TextBox9.Text & "#")
            Dim strName As String = parrametre.cbxDevices2.Text()
            Dim iDeviceSpeed As Integer
            If (Not Integer.TryParse(parrametre.cbxDeviceSpeed2.Text, iDeviceSpeed)) Then
                iDeviceSpeed = 0
            End If
            objGsm.Open(strName, "0000", iDeviceSpeed)
            If (objGsm.LastError <> 0) Then
                If (objGsm.LastError = 36103) Then
                    MessageBox.Show("Invalid Pin entered: SIM card can be blocked after a number of false attempts in a row.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
     
        End Sub
     
        Sub soldDJEZZY()
     
            ' Sends the USSD Command though the selected GSM Modem
     
            If (objGsm.LastError = 0) Then
                objGsm.SendCommand(strCommand)
            End If
            ' Reads the response from the GSM Modem
            If (objGsm.LastError = 0) Then
                strResponse = objGsm.ReadResponse(10000)
            End If
     
            If (objGsm.LastError = 0) Then
     
                If (strResponse.Contains("OK")) Then ' Response should be OK
                    objGsm.SendCommand(String.Empty)
                    strResponse = objGsm.ReadResponse(10000)
     
                    If (objGsm.LastError <> 0) Then
                        UpdateResult2(objGsm.LastError)
                        Return
                    End If
     
                    If (strResponse.Contains("+CUSD:")) Then
     
                        strFields = strResponse.Split(Char.Parse(Chr(34)))
     
                        If (strFields.Length > 1) Then
                            strResponse = strFields(1)
                        Else
                            strResponse = strFields(0)
                        End If
                    End If
                End If
            End If
            BGW.ReportProgress(0, strResponse)
            MsgBox(strResponse)
            UpdateResult2(objGsm.LastError)
            objGsm.Close()
            'Cursor = Cursors.Default
     
        End Sub
    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
    Imports AxMmCtlLib
    Imports System.IO.Ports
    Imports System.Threading
    Imports System.ComponentModel
    Public Class Form1
     
        Dim strResponse As String = String.Empty
        Dim strCommand As String = String.Empty
        Dim strFields As String()
     
        Dim objGsm As Gsm = New Gsm
        Dim objSmsConstants As SmsConstants = New SmsConstants
     
    Sub UpdateResult2(ByVal nResult As Integer)
            'djjizzi résulta
            baha2 = (String.Format("{0}: {1}", nResult, objGsm.GetErrorDescription(objGsm.LastError)))
            TextBox3.Text = Mid(baha2, 4, 8)
        End Sub

  7. #7
    Expert confirmé
    Avatar de wallace1
    Homme Profil pro
    Administrateur systèmes
    Inscrit en
    Octobre 2008
    Messages
    1 966
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Administrateur systèmes
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 966
    Points : 4 005
    Points
    4 005
    Billets dans le blog
    7
    Par défaut
    Ok, peux-tu utiliser les balises code Stp ? merci.

    Donc :

    Je suppose que ceci appel le BGW :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
     
    End Sub
    Le code suivant doit-il être exécuté après le thread (BGW) ? :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    strCommand = String.Format("AT+CUSD=1," & Chr(34) & "{0}" & Chr(34) & ",15", "*" & parrametre.TextBox9.Text & "#")
    Dim strName As String = parrametre.cbxDevices2.Text()
    Dim iDeviceSpeed As Integer
    If (Not Integer.TryParse(parrametre.cbxDeviceSpeed2.Text, iDeviceSpeed)) Then
    iDeviceSpeed = 0
    End If
    objGsm.Open(strName, "0000", iDeviceSpeed)
    If (objGsm.LastError <> 0) Then
    If (objGsm.LastError = 36103) Then
    MessageBox.Show("Invalid Pin entered: SIM card can be blocked after a number of false attempts in a row.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    End If
    Si c'est le cas alors il faut le placer dans l'event Completed !

    Comme je le disais dans mon précédent message : TU NE PEUX PAS METTRE A JOUR LA PARTIE UI DE TON PROGRAMME DEPUIS LE CORPS DE LA METHODE DE TON BGW !!!!!!!
    ---> Cela ne va pas :

    UpdateResult2 est appelé depuis le corps de la méthode BGW (BGW_DoWork -> soldDJEZZY -> UpdateResult2 -> TextBox3.Text) !!!

    Par pitié ajoutes des balises code, merci.

Discussions similaires

  1. Probleme avec la copie des surfaces
    Par Black_Daimond dans le forum DirectX
    Réponses: 3
    Dernier message: 09/01/2003, 10h33
  2. Problèmes avec le filtrage des ip
    Par berry dans le forum Réseau
    Réponses: 9
    Dernier message: 30/12/2002, 07h51
  3. probleme avec la touche F10
    Par b.grellee dans le forum Langage
    Réponses: 2
    Dernier message: 15/09/2002, 22h04
  4. Probleme avec fseek
    Par Bjorn dans le forum C
    Réponses: 5
    Dernier message: 04/08/2002, 07h17
  5. [Kylix] probleme avec un imagelist
    Par NicoLinux dans le forum EDI
    Réponses: 4
    Dernier message: 08/06/2002, 23h06

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