Page 1 sur 1

[R] Annuler un Téléchargement avec compteur (Inet)

Posté : mar. 06 nov. 2007 15:57
par tchomane
Bonjour,

J'essaye en vain d'annuler un téléchargement avec le protocole Inet.

Exemple:

Je télécharge un fichier Vue6PLEWin.zip (350 Mo), un compteur de pourcentage s'affiche dans la barre des tâches.

Je clique sur le bouton annuler, mais ça n'arrête pas le téléchargement... :cry:

Code : Tout sélectionner


#include <GuiConstants.au3>

GuiCreate("Téléchargement", 321, 71,-1, -1)

$Button_1 = GuiCtrlCreateButton("Télécharger", 20, 20, 130, 30)
$Button_2 = GuiCtrlCreateButton("Annuler", 170, 20, 130, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
        
    Case $msg = $Button_1
        
        $url_download = "http://ple.e-onsoftware.com/Vue6PLEWin.zip"

        InetGet($url_download, "c:\Vue6PLEWin.zip", 0, 1)

        While @InetGetActive
        TrayTip("Téléchargement", " " & Round(@InetGetBytesRead / InetGetSize ( $url_download ) *100) & " %", 10, 16)
        Sleep(500)
        Wend

        MsgBox(0, "Téléchargement", "Terminé !")

    Case $msg = $Button_2
        
        InetGet("abort")
        
        MsgBox(0, "Téléchargement", "Annulé !")

    EndSelect
WEnd
Exit

 
Comment peut-on résoudre ce problème?

Merci. :)

Posté : mar. 06 nov. 2007 19:47
par sylvanie
trops de while, tue le while

pour que "Annuler" soit pris en compte il faut être dans la boucle gérant les message de la GUI. Mais en lançant le Download, on est coincé dans le while qui update le traytip

il faut donc passer par un adlibenable à la place de ce deuxième while. celà aura pour effet d'uodater régulièrement le traytip, tout en redonnant la main à la boucle.

Au passage je suis passé par le mode évenementiel, et non le clasique GuiGetMsg, qui dans des cas comme ça peut être peinalisant.

Code : Tout sélectionner

#include <GuiConstants.au3> Opt ( "GUIOnEventMode" , 1 ) ; Change to OnEvent mode $url_download = "http://ple.e - onsoftware.com/Vue6PLEWin.zip" GuiCreate ( "Téléchargement" , 321 , 71 , - 1 , - 1 ) GUISetOnEvent ( $GUI_EVENT_CLOSE , "CLOSEClicked" ) 
$Button_1 = GuiCtrlCreateButton ( "Télécharger" , 20 , 20 , 130 , 30 ) GUICtrlSetOnEvent ( - 1 , "Download" ) 
$Button_2 = GuiCtrlCreateButton ( "Annuler" , 170 , 20 , 130 , 30 ) 
GUICtrlSetOnEvent ( - 1 , "Cancel" ) 
GuiSetState ( ) While 1 Sleep ( 250 ) 
WEnd Func Download ( ) 
InetGet ( $url_download , "c:\Vue6PLEWin.zip" , 0 , 1 ) AdlibEnable ( "update_tray" , 500 ) 
EndFunc 
Func update_tray ( ) 
if @InetGetActive Then 
TrayTip ( "Téléchargement" , " " & Round ( @InetGetBytesRead / InetGetSize ( $url_download ) *100 ) & " %" , 10 , 16 ) Else 
AdlibDisable ( ) 
MsgBox ( 0 , "Téléchargement" , "Terminé !" ) 
EndIf 
EndFunc 
Func Cancel ( ) 
InetGet ( "abort" ) 
TrayTip ( "" , "" , 0 ) 
MsgBox ( 0 , "Téléchargement" , "Annulé !" ) Exit 1 
EndFunc 
Func CLOSEClicked ( ) 
exit 0 
EndFunc

Posté : jeu. 08 nov. 2007 10:15
par tchomane
Sylvanie, t'es un chef ! ça fonctionne !

- Quels sont les avantages, les différences entre le mode évenementiel et le classique GuiGetMsg ?

- A quoi sert le Exit 1 ?


Merci.

- Version avec EventMode :

Code : Tout sélectionner

#include <GuiConstants.au3> 
Opt ( "GUIOnEventMode" , 1 ) ; Change to OnEvent mode 

$url_download = "http://ple.e-onsoftware.com/Vue6PLEWin.zip" 

GuiCreate ( "Téléchargement" , 321 , 71 , - 1 , - 1 )
GUISetOnEvent ( $GUI_EVENT_CLOSE , "CLOSEClicked" ) 
$Button_1 = GuiCtrlCreateButton ( "Télécharger" , 20 , 20 , 130 , 30 ) 
GUICtrlSetOnEvent ( - 1 , "Download" ) 
$Button_2 = GuiCtrlCreateButton ( "Annuler" , 170 , 20 , 130 , 30 ) 
GUICtrlSetOnEvent ( - 1 , "Cancel" ) 
GuiSetState ( ) 

While 1 
    Sleep ( 250 ) 
WEnd 

Func Download ( ) 
InetGet ( $url_download , "c:\Vue6PLEWin.zip" , 0 , 1 ) 
AdlibEnable ( "update_tray" , 500 ) 
EndFunc 

Func update_tray ( ) 
if @InetGetActive Then 
TrayTip ( "Téléchargement" , " " & Round ( @InetGetBytesRead / InetGetSize ( $url_download ) *100 ) & " %" , 10 , 16 ) 
Else 
AdlibDisable ( ) 
MsgBox ( 0 , "Téléchargement" , "Terminé !" ) 
EndIf 
EndFunc 

Func Cancel ( ) 
InetGet ( "abort" ) 
TrayTip ( "" , "" , 0 ) 
MsgBox ( 0 , "Téléchargement" , "Annulé !" ) 
Exit 1 
EndFunc 

Func CLOSEClicked ( ) 
exit 0 
EndFunc
 

- Version Classique GuiGetMsg

Code : Tout sélectionner

#region --- GuiBuilder code Start ---
; Script generated by AutoBuilder 0.6 Prototype

#include <GuiConstants.au3>

GuiCreate("Téléchargement", 321, 71,-1, -1)

$Button_1 = GuiCtrlCreateButton("Télécharger", 20, 20, 130, 30)
$Button_2 = GuiCtrlCreateButton("Annuler", 170, 20, 130, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
        
    Case $msg = $Button_1
        
            $url_download = "http://ple.e-onsoftware.com/Vue6PLEWin.zip"

            InetGet($url_download, "c:\Vue6PLEWin.zip", 0, 1)

            AdlibEnable ( "update_tray" , 500 ) 


    Case $msg = $Button_2
        
            InetGet ( "abort" ) 
            TrayTip ( "" , "" , 0 ) 
            MsgBox ( 0 , "Téléchargement" , "Annulé !" ) 
            Exit 1 

    EndSelect
WEnd
Exit

#endregion --- GuiBuilder generated code End ---


Func update_tray ( ) 
if @InetGetActive Then 
TrayTip ( "Téléchargement" , " " & Round ( @InetGetBytesRead / InetGetSize ( $url_download ) *100 ) & " %" , 10 , 16 ) 
Else 
AdlibDisable ( ) 
MsgBox ( 0 , "Téléchargement" , "Terminé !" ) 
EndIf 
EndFunc 

 

Posté : jeu. 08 nov. 2007 21:18
par sylvanie
le mode par évènement permet d'aléger la boucle de traitement principale quand il y a plein de contrôles (ce qui n'est pas le cas ici) et il est mieux adapté quand il y a des enchaînements d'action avec une cascade de while ou d'adlibenable (c'est pour ça que j'étais passé par là)

après c'est une question de choix.