Bug de label lors d'un téléchargment ftp [VB]

Résolu
Utilisateur anonyme - Modifié le 23 avril 2018 à 17:46
Whismeril Messages postés 19034 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 - 24 avril 2018 à 21:05
Bonjour, j'ai un problème, quand je télécharge un fichier de 2 MO rien ne ce passe par contre quand je télécharge un fichier de 15 MO, à partir de 51% la label qui montre le pourcentage et la label DownloadBytes deviennent grises. Et la progresse bar reste fixe. Mais le téléchargement continue.

Image gif de se que sa fait :



Voici le code :

Imports System.Net
Public Class MJ
 
    Private Sub MJ_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim newFolder As New FolderBrowserDialog
        Control.CheckForIllegalCrossThreadCalls = False
        txtSavePath.Text = newFolder.SelectedPath
        bWorker.RunWorkerAsync()
    End Sub
 
    Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork
        Dim buffer(1023) As Byte
        Dim bytesIn As Integer
        Dim totalBytesIn As Integer
        Dim output As IO.Stream
        Dim flLength As Integer
        Dim Resultat As Integer
        Dim Resultatfinal As Integer
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize
            flLength = CInt(FTPRequest.GetResponse.ContentLength)
            lblFileSize.Location = New Point(574, 260)
            Resultatfinal = flLength / 1048576
            lblFileSize.Text = Resultatfinal & " Mo"
        Catch ex As Exception
 
        End Try
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
            Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
            Dim OutputFilePath As String = Application.StartupPath & "\" & IO.Path.GetFileName(txtFilePath.Text)
            output = System.IO.File.Create(OutputFilePath)
            bytesIn = 1
            Do Until bytesIn < 1
                bytesIn = stream.Read(buffer, 0, 1024)
                If bytesIn > 0 Then
                    output.Write(buffer, 0, bytesIn)
                    totalBytesIn += bytesIn
                    Resultat = totalBytesIn / 1048576
                    lblDownloadedBytes.Text = Resultat.ToString & " Mo"
                    If flLength > 0 Then
                        Dim perc As Integer = (totalBytesIn / flLength) * 100
                        bWorker.ReportProgress(perc)
                    End If
                End If
            Loop
            output.Close()
            stream.Close()
        Catch ex As Exception
            ErreurMessage(ex.Message)
        End Try
    End Sub
 
    Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
        pBar.Value = e.ProgressPercentage
        lblPercent.Text = e.ProgressPercentage.ToString & "%"
    End Sub
 
    Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
        Try
            If IO.File.Exists("VoxaiUpdater.exe") Then
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Voxai.exe", "Voxai(null).exe")
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\VoxaiUpdater.exe", "Voxai.exe")
                Process.Start("Voxai.exe")
                Me.Close()
            Else
                ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
                Me.Close()
            End If
        Catch ex As Exception
            ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
            Me.Close()
        End Try
        FonctionMessage("Téléchargement terminer !")
    End Sub
End Class
A voir également:

2 réponses

Whismeril Messages postés 19034 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 656
23 avril 2018 à 18:25
Bonsoir

tout d'abord, merci à Jordane d'avoir rendu ton code lisible.
Pour tes prochains messages, voir comment faire ici
http://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code attention à bien forcer la coloration en basic.

Commence par virer cette ligne ligne
Control.CheckForIllegalCrossThreadCalls = False
, si ce contrôle existe c'est pas pour rien.
0
Utilisateur anonyme
23 avril 2018 à 19:37
Bonsoir,
Déjà merci pour m'avoir dis comment mettre le code en basic.
Revenons au code, quand je supprime
Control.CheckForIllegalCrossThreadCalls = False
il me met cette erreur :
Opération inter-threads non valide : le côntrole 'IblDownloadBytes' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé.

Merci d'avance pour votre réponse.
0
Whismeril Messages postés 19034 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 656
23 avril 2018 à 20:18
Oui c'est normal.
Tu essaies d'accéder à ce label depuis de bachgroundworker et c'est interdit, car quand on passe outre l'interdiction, ça peut donner des comportements inattendus.
Et là dans le code présenté, à prime abord, je ne voie rien qui explique ton symptôme.
Alors la piste du comportement inattendu est peut être la bonne.

Option 1

Il me semble que ta progressBar ne bouge qu'à partir du moment ou le téléchargement commence, pas quand tu crées le dossier, récupères la taille du fichier et affiches les valeurs => donc ça n'a rien à faire dans la backgroundworker.

Option 2

J'ai mal analysé, il faut que ce soit dans le backgroundworker.
Alors non, pas tout sa position tu peux la fixer avant.
Pour la taille, ReportProgress permet d'ajouter un autre objet.
backgroundWorker1.ReportProgress(-1, Resultatfinal)


Et dans bWorker_ProgressChanged
If e.ProgressPercentage = -1 Then
      lblDownloadedBytes.Text = e.UserState.ToString & " Mo"
      Return
End if

Et tu as plusieurs valeurs à transmettre, tu peux faire un tableau ou une classe dédiée (il faudra un cast pour récupérer le données dans les 2 cas).

Option 3, valable quelque soit le thread

Tu utilise le dispatcher dont le job est de gérer les problèmes inter-thread.

Private leDispatcher As Dispatcher = Dispatcher.CurrentDispatcher

Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
	leDispatcher.Invoke
        (
              New Action
              (
                     Sub() label1.Text = "Coucou depuis le thead"
               )
         )
End Sub

0
J'ai pris l'option 2, les Mo sont bien afficher sa ne devient pas gris par contre la progress bar et le pourcentage ne bouge pas.

Code :
Imports System.Net
Public Class MJ

    Private Sub MJ_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim newFolder As New FolderBrowserDialog
        Control.CheckForIllegalCrossThreadCalls = False
        txtSavePath.Text = newFolder.SelectedPath
        bWorker.RunWorkerAsync()
    End Sub

    Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork
        Dim buffer(1023) As Byte
        Dim bytesIn As Integer
        Dim totalBytesIn As Integer
        Dim output As IO.Stream
        Dim flLength As Integer
        Dim Resultat As Integer
        Dim Resultatfinal As Integer
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize
            flLength = CInt(FTPRequest.GetResponse.ContentLength)
            lblFileSize.Location = New Point(574, 260)
            Resultatfinal = flLength / 1048576
            lblFileSize.Text = flLength & " Mo"
        Catch ex As Exception

        End Try
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
            Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
            Dim OutputFilePath As String = Application.StartupPath & "\" & IO.Path.GetFileName(txtFilePath.Text)
            output = System.IO.File.Create(OutputFilePath)
            bytesIn = 1
            Do Until bytesIn < 1
                bytesIn = stream.Read(buffer, 0, 1024)
                If bytesIn > 0 Then
                    output.Write(buffer, 0, bytesIn)
                    totalBytesIn += bytesIn
                    Resultat = totalBytesIn / 1048576
                    lblDownloadedBytes.Text = Resultat.ToString & " Mo"
                    If flLength > 0 Then
                        Dim perc As Integer = (totalBytesIn / flLength) * 100
                        bWorker.ReportProgress(-1, Resultat)
                    End If
                End If
            Loop
            output.Close()
            stream.Close()
        Catch ex As Exception
            ErreurMessage(ex.Message)
        End Try
    End Sub

    Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
        If e.ProgressPercentage = -1 Then
            lblDownloadedBytes.Text = e.UserState.ToString & " Mo"
            Return
        End If
    End Sub

    Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
        Try
            If IO.File.Exists("VoxaiUpdater.exe") Then
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Voxai.exe", "Voxai(null).exe")
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\VoxaiUpdater.exe", "Voxai.exe")
                Process.Start("Voxai.exe")
                Me.Close()
            Else
                ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
                Me.Close()
            End If
        Catch ex As Exception
            ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
            Me.Close()
        End Try
        FonctionMessage("Téléchargement terminer !")
    End Sub
End Class



Et si possible refait moi le code avec la correction.
0
Whismeril Messages postés 19034 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 656
23 avril 2018 à 21:35
C'est juste cette partie ou il fallait garder ce que tu avais écrit
    Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
        If e.ProgressPercentage = -1 Then
            lblDownloadedBytes.Text = e.UserState.ToString & " Mo"
            Return
        End If
   'code qui affiche la progression

    End Sub


PS
FonctionMessage("Téléchargement terminer !")
terminé
0
Bonjour,

Quand je mis tous les codes il me met une erreur : Le serveur distant a retourné une erreur : 150 6.870 seconds (measured here), 114.31 Kbytes par second.

Code :

Imports System.Net
Public Class MJ

    Private Sub MJ_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim newFolder As New FolderBrowserDialog
        Control.CheckForIllegalCrossThreadCalls = False
        txtSavePath.Text = newFolder.SelectedPath
        bWorker.RunWorkerAsync()
    End Sub

    Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork
        Dim buffer(1023) As Byte
        Dim bytesIn As Integer
        Dim totalBytesIn As Integer
        Dim output As IO.Stream
        Dim flLength As Integer
        Dim Resultat As Integer
        Dim Resultatfinal As Integer
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize
            flLength = CInt(FTPRequest.GetResponse.ContentLength)
            lblFileSize.Location = New Point(574, 260)
            Resultatfinal = flLength / 1048576
            lblFileSize.Text = Resultatfinal & " Mo"
        Catch ex As Exception
            ErreurMessage(ex.Message)
        End Try
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
            Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
            Dim OutputFilePath As String = Application.StartupPath & "\" & IO.Path.GetFileName(txtFilePath.Text)
            output = System.IO.File.Create(OutputFilePath)
            bytesIn = 1
            Do Until bytesIn < 1
                bytesIn = stream.Read(buffer, 0, 1024)
                If bytesIn > 0 Then
                    output.Write(buffer, 0, bytesIn)
                    totalBytesIn += bytesIn
                    Resultat = totalBytesIn / 1048576
                    lblDownloadedBytes.Text = Resultat.ToString & " Mo"
                    If flLength > 0 Then
                        Dim perc As Integer = (totalBytesIn / flLength) * 100
                        bWorker.ReportProgress(-1, Resultat)
                    End If
                End If
            Loop
            output.Close()
            stream.Close()
        Catch ex As Exception
            ErreurMessage(ex.Message)
        End Try
    End Sub

    Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
        If e.ProgressPercentage = -1 Then
            lblDownloadedBytes.Text = e.UserState.ToString & " Mo"
            Return
        End If
        pBar.Value = e.ProgressPercentage
        lblPercent.Text = e.ProgressPercentage.ToString & "%"
    End Sub

    Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
        Try
            If IO.File.Exists("VoxaiUpdater.exe") Then
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Voxai.exe", "Voxai(null).exe")
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\VoxaiUpdater.exe", "Voxai.exe")
                Process.Start("Voxai.exe")
                Me.Close()
            Else
                ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
                Me.Close()
            End If
        Catch ex As Exception
            ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
            Me.Close()
        End Try
        FonctionMessage("Téléchargement terminer !")
    End Sub
End Class



Cordialement,
Locod.
0
Utilisateur anonyme
24 avril 2018 à 13:13
Bonjour,

J'ai régler le bug mais la progress bar et la label reste fixe même avec le code que j'ai supprimé.

Code :

Imports System.Net
Public Class MJ

    Private Sub MJ_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim newFolder As New FolderBrowserDialog
        Control.CheckForIllegalCrossThreadCalls = False
        txtSavePath.Text = newFolder.SelectedPath
        bWorker.RunWorkerAsync()
    End Sub

    Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork
        Dim buffer(1023) As Byte
        Dim bytesIn As Integer
        Dim totalBytesIn As Integer
        Dim output As IO.Stream
        Dim flLength As Integer
        Dim Resultat As Integer
        Dim Resultatfinal As Integer
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize
            flLength = CInt(FTPRequest.GetResponse.ContentLength)
            lblFileSize.Location = New Point(574, 260)
            Resultatfinal = flLength / 1048576
            lblFileSize.Text = Resultatfinal & " Mo"
        Catch ex As Exception
            ErreurMessage(ex.Message)
        End Try
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
            Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
            Dim OutputFilePath As String = Application.StartupPath & "\" & IO.Path.GetFileName(txtFilePath.Text)
            output = System.IO.File.Create(OutputFilePath)
            bytesIn = 1
            Do Until bytesIn < 1
                bytesIn = stream.Read(buffer, 0, 1024)
                If bytesIn > 0 Then
                    output.Write(buffer, 0, bytesIn)
                    totalBytesIn += bytesIn
                    Resultat = totalBytesIn / 1048576
                    If flLength > 0 Then
                        Dim perc As Integer = (totalBytesIn / flLength) * 100
                        bWorker.ReportProgress(-1, Resultat)
                    End If
                End If
            Loop
            output.Close()
            stream.Close()
        Catch ex As Exception
            ErreurMessage(ex.Message)
        End Try
    End Sub

    Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
        If e.ProgressPercentage = -1 Then
            lblDownloadedBytes.Text = e.UserState.ToString & " Mo"
            Return
        End If
        pBar.Value = e.ProgressPercentage
        lblPercent.Text = e.ProgressPercentage.ToString & "%"
    End Sub

    Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
        Try
            If IO.File.Exists("VoxaiUpdater.exe") Then
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Voxai.exe", "Voxai(null).exe")
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\VoxaiUpdater.exe", "Voxai.exe")
                Process.Start("Voxai.exe")
                Me.Close()
            Else
                ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
                Me.Close()
            End If
        Catch ex As Exception
            ErreurMessage("Erreur lors de l'installation de la mise à jour Voxai")
            Me.Close()
        End Try
        FonctionMessage("Téléchargement terminer !")
    End Sub
End Class



Cordialement,
Locod.
0
Utilisateur anonyme
24 avril 2018 à 14:53
Bonsoir,

J'ai trouvé le problème tous seul, enfaite il fallait crée un timer .

An-revoir et merci Whismeril.
0
Rejoignez-nous