Jump to content

InetClose error


 Share

Recommended Posts

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#include <Math.au3>

#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1", 616, 194, 544, 139)
$gProgress = GUICtrlCreateProgress(56, 120, 473, 49)
$download = GUICtrlCreateButton("download", 160, 40, 265, 49)
$cancel = GUICtrlCreateButton("cancel", 552, 120, 49, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $download
            _Download("https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe", @TempDir & "\Setup.exe")
        Case $cancel
            _cancel()
    EndSwitch
WEnd

Func _cancel()
    Local $hhdownload = InetGet("https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe", @TempDir & "\Setup.exe", 1, 1)
    InetClose($hhDownload)
    FileDelete(@TempDir & "\Setup.exe")
EndFunc

Func _Download($address, $dest)
$_FinalUrl = 'https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe'
$_TempPath = @TempDir & '\setup.exe'
$_FileSize = InetGetSize ( $_FinalUrl )
$_Download = InetGet ( $_FinalUrl, $_TempPath, 1, 1 )
Local $_InfoData
Do
    $_InfoData = InetGetInfo ( $_Download )
    If Not @error Then
        $_InetGet = $_InfoData[0]
        $_DownloadPercent = Round ( ( 100 * $_InetGet ) / $_FileSize )
        $_DownloadPercent = _Min ( _Max ( 1, $_DownloadPercent ), 99 )
        GUICtrlSetData ( $gProgress, $_DownloadPercent )
        $_Label = GUICtrlCreateLabel ( 'Mozilla downloading... : ' & ' %' & $_DownloadPercent, 142, 355, 212, 12)
    EndIf
    Sleep ( 100 )
Until $_InfoData[2] = True
EndFunc

why cancel button doesn't work?

Link to comment
Share on other sites

Well the answer to you rather vague question is that your cancel button works just fine.

But I think you'll need to wait until a download actually starts before you can cancel it.

Example might be to alter the function...

#include <InetConstants.au3>
Func _cancel()
    Local $hhdownload = InetGet("https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe", @TempDir & "\Setup.exe", 1, 1)

    Do
        Sleep(250)
    Until InetGetInfo($hhdownload, $INET_DOWNLOADREAD) > 0

    InetClose($hhDownload)
    FileDelete(@TempDir & "\Setup.exe")
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

 

Well the answer to you rather vague question is that your cancel button works just fine.

But I think you'll need to wait until a download actually starts before you can cancel it.

Example might be to alter the function...

#include <InetConstants.au3>
Func _cancel()
    Local $hhdownload = InetGet("https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe", @TempDir & "\Setup.exe", 1, 1)

    Do
        Sleep(250)
    Until InetGetInfo($hhdownload, $INET_DOWNLOADREAD) > 0

    InetClose($hhDownload)
    FileDelete(@TempDir & "\Setup.exe")
EndFunc

Tried. not working

Link to comment
Share on other sites

Well you have a number of problems with that. First and foremost, when that download begins it is then stuck in a Do...Until loop until it finished.

There are a number of ways you can fix this, one being to check the Gui msg in that loop and another is to check the download status in the main GUI loop.

And for sanity sake, remove the download you start in the _cancel function.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

implementing J1's suggestion...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#include <Math.au3>

#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1", 616, 194, 544, 139)
$gProgress = GUICtrlCreateProgress(56, 120, 473, 49)
$download = GUICtrlCreateButton("download", 160, 40, 265, 49)
$cancel = GUICtrlCreateButton("cancel", 552, 120, 49, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $download
            _Download("https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe", @TempDir & "\Setup.exe")
    EndSwitch
WEnd

Func _Download($address, $dest)
    $_FinalUrl = 'https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/win32/tr/Firefox%20Setup%2032.0.exe'
    $_TempPath = @TempDir & '\setup.exe'
    $_FileSize = InetGetSize($_FinalUrl)
    $_Download = InetGet($_FinalUrl, $_TempPath, 1, 1)
    Local $_InfoData
    Do
        If GUIGetMsg() = $cancel Then
            InetClose($_Download)
            FileDelete(@TempDir & "\Setup.exe")
            ConsoleWrite('Download cancelled' & @CRLF)
            Return
        EndIf
        $_InfoData = InetGetInfo($_Download)
        If Not @error Then
            $_InetGet = $_InfoData[0]
            $_DownloadPercent = Round((100 * $_InetGet) / $_FileSize)
            $_DownloadPercent = _Min(_Max(1, $_DownloadPercent), 99)
            GUICtrlSetData($gProgress, $_DownloadPercent)
            $_Label = GUICtrlCreateLabel('Mozilla downloading... : ' & ' %' & $_DownloadPercent, 142, 355, 212, 12)
        EndIf
        Sleep(100)
    Until $_InfoData[2] = True
EndFunc   ;==>_Download

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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...