Jump to content

cancel download button


Fran
 Share

Recommended Posts

I am struggling to cancel my download... I'm still a bit of a newbie when it comes to certain things.

I've checked the help file, but I just can't get the Cancel button to interrupt the download.

Hope someone can help me ;)

#include <Math.au3>
#include <GUIConstantsEx.au3>
_Example1()

Func _Example1()
$_DownloadPercent=0
$_Gui = GUICreate ( "" )
$butCancel = GUICtrlCreateButton("Cancel", 350/2, 100)
$_ProgressBar = GUICtrlCreateProgress ( 5, 25, 350, 23 )
$_Label = GUICtrlCreateLabel ($_DownloadPercent & ' %', 360, 30, 40,20 )
GUISetState ( @SW_SHOW )

$_FinalUrl = 'http://www.shapecollage.com/2.5/ShapeCollage-2.5-Setup.exe'
$_TempPath = @scriptDir & "\ShapeCollage-2.5-Setup.exe"
$_FileSize = InetGetSize ( $_FinalUrl )
$_Label2 = GUICtrlCreateLabel ("Progress:", 5, 60, 50,20 )
$_Label2 = GUICtrlCreateLabel ("0KB", 52, 60, 200,20 )
$_Download = InetGet ( $_FinalUrl, $_TempPath, 1, 1 )
Switch GUIGetMsg()
    Case $butCancel
        InetClose($_Download)
EndSwitch

Local $_InfoData
Do
     Sleep ( 500 )
    $_InfoData = InetGetInfo ( $_Download )
    If Not @error Then
        $_InetGet = $_InfoData[0]
        $_DownloadPercent = Round ( ( 100 * $_InetGet ) / $_FileSize )
        $_DownloadPercent = _Min ( _Max ( 1, $_DownloadPercent ), 99 )
        GUICtrlSetData ( $_ProgressBar, $_DownloadPercent)
        GUICtrlSetData ( $_Label, $_DownloadPercent & ' %')
        GUICtrlSetData ($_Label2, $_InetGet/1024 & "KB of "& Round($_FileSize/1024) & "KB downloaded")
    EndIf
Until $_InfoData[2] = True
GUICtrlCreateLabel ( 'Download successfull !', 5, 60, 350, 20 )
Sleep ( 2000 )

EndFunc
Link to comment
Share on other sites

I wrote some time ago this:

;Coded by UEZ 2010
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

Local $hGUI = GUICreate("Downloader by UEZ 2010", 615, 267, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
Local $Input_URL = GUICtrlCreateInput("http://www.autoitscript.com/autoit3/files/beta/autoit/autoit-v3.3.5.6-sfx.exe", 64, 24, 497, 21)
Local $Label1 = GUICtrlCreateLabel("URL:", 32, 26, 29, 17)
Local $Start_DL = GUICtrlCreateButton("Download", 64, 128, 75, 25)
GUICtrlSetOnEvent(-1, "Start_DL")
Local $Stop_DL = GUICtrlCreateButton("Cancel", 488, 128, 75, 25)
GUICtrlSetOnEvent(-1, "Stop_DL")
GUICtrlSetState(-1, $GUI_DISABLE)
Local $Progress = GUICtrlCreateProgress(8, 192, 598, 17)
Local $StatusBar = _GUICtrlStatusBar_Create($hGUI)
_GUICtrlStatusBar_SetText($StatusBar, "Ready")
Local $Input_Save_To = GUICtrlCreateInput(@ScriptDir, 64, 80, 497, 21)
Local $Label2 = GUICtrlCreateLabel("Save to:", 16, 84, 44, 17)
GUISetState(@SW_SHOW)


Local $hDownload, $nBytes, $nRead, $nSize, $calc, $file, $url
Local $prog = 0
Local $stop = 0

While Sleep(1000000000)
WEnd

Func Start_DL()
    $url = GUICtrlRead($Input_URL)
    $file = GUICtrlRead($Input_Save_To)
    If $url <> "" And $file <> "" Then
    GUICtrlSetState($Start_DL, $GUI_DISABLE)
    GUICtrlSetState($Stop_DL, $GUI_ENABLE)
    $file &= "\" & StringRight($url, StringLen($url) - StringInStr($url, "/", 0, -1))
    $nSize = InetGetSize($url)
    $hDownload = InetGet($url, $file, 1, 1)
    AdlibRegister("DL_Check", 50)
    EndIf
EndFunc

Func DL_Check()
    If InetGetInfo($hDownload, 2) Or $stop Then
    InetClose($hDownload)
    GUICtrlSetState($Start_DL, $GUI_ENABLE)
    GUICtrlSetState($Stop_DL, $GUI_DISABLE)
    GUICtrlSetData($Progress, 0)
    _GUICtrlStatusBar_SetText($StatusBar, "Ready")
    $stop = 0
    AdlibUnRegister("DL_Check")
    Else
    $nRead = InetGetInfo($hDownload, 0)
    $calc = Int(100 * $nRead / $nSize)
    GUICtrlSetData($Progress, $calc)
    _GUICtrlStatusBar_SetText($StatusBar, $nRead & " / " & $nSize & " bytes")
    EndIf
EndFunc

Func Stop_DL()
    $stop = 1
EndFunc

Func _Exit()
    InetClose($hDownload)
    GUIDelete($hGUI)
    Exit
EndFunc

Maybe it will help you.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Another question... is there a way to pause the download?

Reason I'm asking is, I want the user to confirm whether they want to cancel the download.

When I add a msgbox within your Stop_DL Function, the download keeps running though.

F

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...