Barre de progression sans fin inopérante, remplacé par PictureBox et gif

little boy62 Messages postés 3584 Date d'inscription lundi 11 novembre 2013 Statut Membre Dernière intervention 15 avril 2024 - Modifié le 25 oct. 2020 à 13:54
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 25 oct. 2020 à 15:23
Bonjour.

De base, je cherchais avant tout chose :
- de créer une barre de progression sans fin et de la faire défiler
- de faire les actions désirées
- d'arrêter le défilement de la dite barre.

En AutoIt, c'était relativement simple...


Mais là, je me heurte à un mur en béton armé. XD


En effet, j'ai testé divers barres de progression....
Aucune barre ne défilait.... sauf quand les actions d’après était terminées....


j'en ai eu marre, je me suis donc lancé à l'ajout d'un gif animé via un PictureBox.
Ca fonctionne, seul.

Mais dès que je rajoute du code ensuite, comme pour la barre de progression, l'image gif n'est pas animée.
=> j'enrage.^^


Premier code avec barre de défilement

Imports System.Net
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Form1

    Dim dossier_res = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YTDL-FFBIN"
    Dim YTDL = dossier_res & "\youtube-dl.exe"
    Dim FFBIN_url = "https://ffbinaries.com/downloads"

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' débyt barre de défilement
        ProgressBar1.MarqueeAnimationSpeed = 100

        If System.IO.Directory.Exists(dossier_res) Then System.IO.Directory.Delete(dossier_res, True)
        Directory.CreateDirectory(dossier_res)
        DownloadYTDL(YTDL, "https://yt-dl.org/latest/youtube-dl.exe")
        DownloadFFBinaries(FFBIN_url)
        Process.Start(dossier_res)


        ' fin barre de défilement

    End Sub

    Public Sub DownloadYTDL(fichier As String, lien As String)
        Dim client As New WebClient
        client.Credentials = New NetworkCredential()
        client.DownloadFile(lien, fichier)
    End Sub

    Public Sub DownloadFFBinaries(lien)

        Dim sb As New StringBuilder
        Dim sb2 As New StringBuilder
        Dim sb3 As New StringBuilder
        Dim html As String

        Dim web_client As WebClient = New WebClient
        Using wc As New WebClient
            html = wc.DownloadString(lien)
        End Using

        Dim regx As New Regex("<?href\s*=\s*[""'].+?[""'][^>]*?", RegexOptions.IgnoreCase)
        Dim matches As MatchCollection = regx.Matches(html)

        For Each match As Match In matches
            If match.Value.Contains("64.zip") And match.Value.Contains("ffmpeg") Then
                Dim line = match.Value.Remove(0, 6)
                line = line.Remove(line.Length - 1, 1)
                'match.Value.Replace("href=", "")
                sb.AppendLine(line)
                Exit For
            End If
        Next

        For Each match As Match In matches
            If match.Value.Contains("64.zip") And match.Value.Contains("ffprobe") Then
                Dim line = match.Value.Remove(0, 6)
                line = line.Remove(line.Length - 1, 1)
                'match.Value.Replace("href=", "")
                sb2.AppendLine(line)
                Exit For
            End If
        Next

        For Each match As Match In matches
            If match.Value.Contains("64.zip") And match.Value.Contains("ffplay") Then
                Dim line = match.Value.Remove(0, 6)
                line = line.Remove(line.Length - 1, 1)
                'match.Value.Replace("href=", "")
                sb3.AppendLine(line)
                Exit For
            End If
        Next

        Dim lien_ffmpeg = sb.ToString
        Dim lien_ffprobe = sb2.ToString
        Dim lien_ffplay = sb3.ToString
        Dim fichier_ffmpeg = dossier_res & "\ffmpeg.zip"
        Dim fichier_ffprobe = dossier_res & "\ffprobe.zip"
        Dim fichier_ffplay = dossier_res & "\ffplay.zip"

        Dim client2 As New WebClient
        client2.Credentials = New NetworkCredential()
        client2.DownloadFile(lien_ffmpeg, fichier_ffmpeg)
        client2.DownloadFile(lien_ffprobe, fichier_ffprobe)
        client2.DownloadFile(lien_ffplay, fichier_ffplay)



    End Sub



End Class



la barre défile après que toutes les actions ont été réalisées...


Code avec PictureBox

Imports System.Net
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Form1

    Dim dossier_res = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YTDL-FFBIN"
    Dim YTDL = dossier_res & "\youtube-dl.exe"
    Dim FFBIN_url = "https://ffbinaries.com/downloads"

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' débyt barre de défilement
        PictureBox1.Show()


        If System.IO.Directory.Exists(dossier_res) Then System.IO.Directory.Delete(dossier_res, True)
        Directory.CreateDirectory(dossier_res)
        DownloadYTDL(YTDL, "https://yt-dl.org/latest/youtube-dl.exe")
        DownloadFFBinaries(FFBIN_url)
        Process.Start(dossier_res)

        PictureBox1.Hide()

        ' fin barre de défilement

    End Sub

    Public Sub DownloadYTDL(fichier As String, lien As String)
        Dim client As New WebClient
        client.Credentials = New NetworkCredential()
        client.DownloadFile(lien, fichier)
    End Sub

    Public Sub DownloadFFBinaries(lien)

        Dim sb As New StringBuilder
        Dim sb2 As New StringBuilder
        Dim sb3 As New StringBuilder
        Dim html As String

        Dim web_client As WebClient = New WebClient
        Using wc As New WebClient
            html = wc.DownloadString(lien)
        End Using

        Dim regx As New Regex("<?href\s*=\s*[""'].+?[""'][^>]*?", RegexOptions.IgnoreCase)
        Dim matches As MatchCollection = regx.Matches(html)

        For Each match As Match In matches
            If match.Value.Contains("64.zip") And match.Value.Contains("ffmpeg") Then
                Dim line = match.Value.Remove(0, 6)
                line = line.Remove(line.Length - 1, 1)
                'match.Value.Replace("href=", "")
                sb.AppendLine(line)
                Exit For
            End If
        Next

        For Each match As Match In matches
            If match.Value.Contains("64.zip") And match.Value.Contains("ffprobe") Then
                Dim line = match.Value.Remove(0, 6)
                line = line.Remove(line.Length - 1, 1)
                'match.Value.Replace("href=", "")
                sb2.AppendLine(line)
                Exit For
            End If
        Next

        For Each match As Match In matches
            If match.Value.Contains("64.zip") And match.Value.Contains("ffplay") Then
                Dim line = match.Value.Remove(0, 6)
                line = line.Remove(line.Length - 1, 1)
                'match.Value.Replace("href=", "")
                sb3.AppendLine(line)
                Exit For
            End If
        Next

        Dim lien_ffmpeg = sb.ToString
        Dim lien_ffprobe = sb2.ToString
        Dim lien_ffplay = sb3.ToString
        Dim fichier_ffmpeg = dossier_res & "\ffmpeg.zip"
        Dim fichier_ffprobe = dossier_res & "\ffprobe.zip"
        Dim fichier_ffplay = dossier_res & "\ffplay.zip"

        Dim client2 As New WebClient
        client2.Credentials = New NetworkCredential()
        client2.DownloadFile(lien_ffmpeg, fichier_ffmpeg)
        client2.DownloadFile(lien_ffprobe, fichier_ffprobe)
        client2.DownloadFile(lien_ffplay, fichier_ffplay)



    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Hide()
    End Sub
End Class


L'image gif n'est pas animée.


Deux questions :
- Comment faire pour que la barre de défile défile à l'appui sur le bouton ?
- Comment faire pour que le gif animé (PictureBox) défile à l'appui sur le bouton ?


J'ai comme l'impression qu'il faut que la barre de progression / picturebox doit avoir un paramètre pour qu'il puisse fonctionner.


Merci de m'aider.

++

3 réponses

Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
25 oct. 2020 à 14:20
Bonjour

c'est parce tout est exécuté dans le même thread.
Donc la progression ne commence que quand tout le code est exécuté.

Il faut que tu fasses les 2 choses dans des threads séparés.
Et de préférence la progressbar dans le thread principal, car seul ce thread a accès à l'ihm.
C'est possible qu'un thread secondaire demande une modification de l'ihm au thread principal, mais c'est se compliquer la vie, et dans ton cas ça ne me parait pas justifié.

Tu peux te servir d'un backgroundworker qui est un objet qui gère le multithread pour executer tes téléchargements.
Cet objet est simple à utiliser mais son thread n'a pas une priorité importante.
Un exemple ici https://codes-sources.commentcamarche.net/forum/affich-10102660-thread-et-maj-de-control-dans-un-form#5


Si tu veux gérer la priorité d'exécution, il te faudra créer des threads "à la main", là il te faut un peu plus étudier le domaine
https://lite.qwant.com/?q=multithread+dot+net&client=opensearch

Ou alors avec la fabrique de threads
https://fdorin.developpez.com/tutoriels/csharp/threadpool/part1/


0
little boy62 Messages postés 3584 Date d'inscription lundi 11 novembre 2013 Statut Membre Dernière intervention 15 avril 2024
25 oct. 2020 à 15:13
Bonjour et merci pour ta réponse.

J'avais déjà lu et vu des sujets parlant des background worker...


Ta réponse m'a fait faire une énième recherche, PictureBox avec BackgroundWoker.
Je n'avais pas fait cette recherche avec la barre de progession.

Et je suis tombé sur ça :
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/2d6cf660-8ee7-41d6-9a53-d4bbeb151d4f/picture-box-is-not-showing-up-on-button-click-event?forum=vbgeneral

Et donc, je l'ai adapté par ceci :
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' débyt barre de défilement
        PictureBox1.Show()

        Await Task.Run(
            Sub()
                If System.IO.Directory.Exists(dossier_res) Then System.IO.Directory.Delete(dossier_res, True)
                Directory.CreateDirectory(dossier_res)
                DownloadYTDL(YTDL, "https://yt-dl.org/latest/youtube-dl.exe")
                DownloadFFBinaries(FFBIN_url)
                Process.Start(dossier_res)
            End Sub)

        PictureBox1.Hide()

        ' fin barre de défilement

    End Sub


Ca fonctionne !


Et donc, je l'ai aussi adapté avec la barre de défilement que je voulais
   Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        ' ProgressBar1.Style = ProgressBar1.Style.Marquee
        ProgressBar1.MarqueeAnimationSpeed = 100

        Await Task.Run(
            Sub()
                If System.IO.Directory.Exists(dossier_res) Then System.IO.Directory.Delete(dossier_res, True)
                Directory.CreateDirectory(dossier_res)
                DownloadYTDL(YTDL, "https://yt-dl.org/latest/youtube-dl.exe")
                DownloadFFBinaries(FFBIN_url)
                Process.Start(dossier_res)
            End Sub)


        ProgressBar1.MarqueeAnimationSpeed = 0



    End Sub




Ca fonctionne aussi.



Est-ce une bonne méthode ?

++


0
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
Modifié le 25 oct. 2020 à 15:24
Oui et non.

Le principal intérêt de la fabrique de thread consiste à disposer d’un stock de threads prêts à servir, c’est donc utile pour gagner du temps quand on doit enchaîner les threads.
Un autre avantage c’est que ça se code facilement.

Par contre, les erreurs d’exécution ne sont pas remontées.
Tu ne sauras pas si ton téléchargement est finit ou s’il a planté quand ça s’arrêtera.
C’est très bien décrit dans les articles de fdorin que je t’ai mis en lien

0
Rejoignez-nous