Download_demo (simulation de dl avec gestion des % et tps restant)

0/5 (2 avis)

Vue 5 061 fois - Téléchargée 541 fois

Description

Cette source à pour but de simuler le download de fichiers contenu dans un listview. L'intérêt est voir l'utilisation d'un timer, d'un contrôle listview et des calculs sur les durées.

Source / Exemple :


' ==========================================================
' DOWNLOAD_DEMO (Freeware)
' Version 1.00 du 29/12/2002
' By Indi59 (c)
' Mail : indianajones59@msn.com
' ==========================================================
' Ce programme a pour modeste but de présenter l'utilisation
' d'un temps restant en fonction du Ko/s pour un Download et
' le traitement d'un controle Listview.
' ==========================================================
Dim recu As Long
Dim TotalRecu As Long
Dim duree As Long
Dim fichier(100) As String
Dim taille(100) As Long
Dim NbFichier As Integer
Dim FichierEnCours As Integer
Private Sub Command1_Click()
If Command1.Caption = "Pause" Then Timer1.Enabled = False: Command1.Caption = "Reprise": Exit Sub
If Command1.Caption = "Reprise" Then Timer1.Enabled = True: Command1.Caption = "Pause": Exit Sub
If Command1.Caption = "Go" Then LanceFichier (1): TotalRecu = 0: Label12 = 0: Timer1.Enabled = True: Command1.Caption = "Pause": Exit Sub
End Sub
'permet de formatter une chaine separé d'un espace tous les 3 caractères
Public Function Formatte(entree As String) As String
Formatte = Trim(Format(entree, "### ### ### ### ### ### ### ### ### ###"))
End Function
Private Sub init()
initFile
Ko_s_min = 14 '14,00 Ko/s
ko_s_max = 16 '16,00 Ko/s
Label2.Caption = Str(Ko_s_min / 102.4) + " ko/s"
Label1 = "0"
Label7 = "0"
Label10 = "00:00:00": Label11 = "00:00:00"
VScroll1.max = Ko_s_min * 102.4
VScroll1.Value = ko_s_max * 102.4
Timer1.Enabled = False: Command1.Caption = "Go"
End Sub
Private Sub initFile()
'Ici les fichiers a traiter se font du manière manuelle
'mais on peut imaginer l'utilisation de d'un treeview
'===============================================
NbFichier = 3
fichier(1) = "Fichier1.exe": taille(1) = 1000000
fichier(2) = "Fichier2.exe": taille(2) = 5000000
fichier(3) = "Fichier3.exe": taille(3) = 9876543
'===============================================
ListView1.ListItems.Clear
tailletotal = 0
For t = 1 To NbFichier
tailletotal = tailletotal + taille(t)
ListView1.ListItems.Add , , fichier(t)
x = ListView1.ListItems.Count
ListView1.ListItems(x).ListSubItems.Add , , taille(t)
ListView1.ListItems(x).ListSubItems.Add , , " "
Next t
Label8 = Formatte(Str(tailletotal)) + " octets."
Label6 = "Fichier 1/ " & NbFichier: Bar2.max = tailletotal
End Sub

Private Sub form_load()
init
End Sub
'Transforme un paquet de secondes en HH:MM:SS
'Exemple 65 -> 1 minutes et 5 secondes soit 00:01:05
Public Function FormatteH(entree As String) As String
sec = Int(entree)
h = Int(sec / 3600)
restem = sec - (3600 * h)
If restem < 60 Then m = 0: GoTo suite
m = Int(restem / 60)
suite:
restes = sec - (3600 * h) - (m * 60)
retour = Format(h, "#00") & ":" & Format(m, "#00") & ":" & Format(restes, "#00")
FormatteH = retour
End Function
'Un click sur le label2 permet d'entrée un taux de son choix entre 0 et 300 Ko/s
'l'entrée deviens la valeur minimum du VScroll1
Private Sub Label2_Click()
rep = InputBox("Entrez une valeur sans Ko/s" + Chr$(13) + "Exemple : 15", "Ko/s = Saisie Manuelle")
If rep >= 300 Then rep = 300
If rep <= 0 Then rep = 16
VScroll1.max = Int(rep) * 102.4
VScroll1.Value = Int(rep) * 102.4
VScroll1_Change
End Sub
' Fin du traitement des n fichiers
Private Sub fin()
Bar1.Value = 0
Bar2.Value = 0
Frame2.Caption = "00:00:00"
Label1 = "0 octet(s)"
Label7 = "0 octet(s)"
Label3 = ""
Label4 = "0 octet(s)"
Label6 = ""
Command1.Caption = "Go"
Timer1.Enabled = False
End Sub
'Traitement du fichier N dans le listview1
Private Sub LanceFichier(NumFichierEnCours As Integer)
FichierEnCours = NumFichierEnCours
TotalRecu = 0
For z = 1 To FichierEnCours - 1
TotalRecu = TotalRecu + taille(z)
Next z
Label7 = Formatte(Str(TotalRecu)) + " octets"
If FichierEnCours > NbFichier Then fin: Exit Sub
Label6 = "Fichier " & FichierEnCours & "/" & NbFichier
Bar1.Value = 0
Bar1.max = taille(FichierEnCours)
recu = 0
Bar1.ToolTipText = "0,00%"
Label4 = Formatte(Str(ListView1.ListItems(FichierEnCours).ListSubItems(1).Text)) + " octets."
Label3 = fichier(FichierEnCours)
End Sub
'Ici on traite les octets recu toutes les secondes (Intervalle 1000 millisecondes soit 1 seconde)
Private Sub Timer1_Timer()
duree = duree + 1 '(seconde écoulée depuis le debut du traitement)
Frame2.Caption = FormatteH(Str(duree))
recu1 = VScroll1.Value 'octets du fichier en cours
recu2 = recu1 * 10
recu = recu + recu2
If recu >= Bar1.max Then recu = Bar1.max
TotalRecu = TotalRecu + recu2

If TotalRecu > Bar2.max Then TotalRecu = Bar2.max
Bar2.Value = TotalRecu
pourcentage1 = (recu / Bar1.max) * 100
pourcentage2 = (TotalRecu / Bar2.max) * 100
If pourcentage1 > 100 Then pourcentage1 = 100
Bar1.ToolTipText = Format(pourcentage1, "#.##") & " %"
Bar2.ToolTipText = Format(pourcentage2, "#.##") & " %"
Label1 = Formatte(Str(recu)) + " octets."
Label7 = Formatte(Str(TotalRecu)) + " octets."
valeur = VScroll1.Value
'valeur = valeur * 10
Label13 = TpsRestant(Int(valeur), recu, taille(FichierEnCours))
Label10 = TpsRestant(Int(valeur), Bar2.Value, Bar2.max)
If recu >= Bar1.max Then
 Bar1.Value = Bar1.max
 Label1.Caption = Label4.Caption
 ListView1.ListItems(FichierEnCours).ListSubItems(2).Text = "Terminé"
 LanceFichier (FichierEnCours + 1)
 Exit Sub
End If
Bar1.Value = recu
ListView1.ListItems(FichierEnCours).ListSubItems(2).Text = Bar1.ToolTipText
End Sub

Private Sub VScroll1_Change()
    valeur = VScroll1.Value
    valeurs = valeur / 102.4
    Label2.Caption = Format(valeurs, "#0.00") + " Ko/s"
End Sub

Private Sub VScroll1_Scroll()
    VScroll1_Change
End Sub
'Ici on calcule le temps nécessaire pour recuperer un nombre d'octets par
'rapport au nombre Ko/s
'Ainsi pour 10 Ko/s -> 1024 octets seront recuperés toutes les 1 seconde
Public Function TpsRestant(Kos As Long, actu As Long, max As Long) As String
tailleRestant = max - actu
Kos_oct = Kos
If tailleRestant < Kos_oct Then TpsRestant = 0: Exit Function
TpsRestant = FormatteH(Int(tailleRestant / Kos_oct))
End Function

Conclusion :


Je ne suis pas un développeur hors paire, alors sans doute qu'il y a mieux à faire, ne vous genez pas...

Si vous désirez me joindre : indianajones59@msn.com

Codes Sources

A voir également

Ajouter un commentaire Commentaires
cs_Jackboy Messages postés 757 Date d'inscription vendredi 7 septembre 2001 Statut Membre Dernière intervention 19 juin 2008
6 août 2004 à 17:06
j' aimerai savoir s'il est pensable, a l'aide de ton appli, de faire un vrai service de downlaod, pas qu'une simulation...
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
29 déc. 2002 à 13:20
franchement un grand merci pour ta source ke je trouve tres bien... elle pourrait en plus mettre utile... merci... 8/10

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.