Jump to content
ATR

Call a function with switch/case

Recommended Posts

ATR

Hello,

I have a problem with my script. I have a GUI and condition with switch / case / endswitch

I call a function if we clicked the "download" button, andwe click an other button, we must wait until the first case statement is not finished !!

#include <GUIConstantsEx.au3>
#include <Inet.au3>

Global $Gui = GUICreate("", 250, 100)

$Specs = GUICtrlCreateButton("Download", 20, 70, 100, 26)
$Open_Folder = GUICtrlCreateButton("Open folder", 140, 70, 100, 26)

Global $Download_Folder = @DesktopDir & "\"

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Specs
            Call(Test_1())
            ContinueLoop
        Case $Open_Folder
            ShellExecute(@DesktopDir & "\")
    EndSwitch
WEnd

GUIDelete($Gui)


Func Test_1()
    Local $File_Size = InetGetSize("http://updates3.safer-networking.org/spybot1/spybotsd162.exe", $INET_FORCERELOAD)
    ConsoleWrite($File_Size & @LF)
    $Download = InetGet("http://updates3.safer-networking.org/spybot1/spybotsd162.exe", $Download_Folder & "Spybot162.exe", $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
    $Info = InetGetInfo($Download)
    If @error Then
        Return 0
    Else
        While Not InetGetInfo($Download, $INET_DOWNLOADCOMPLETE)
            Sleep(100)
        WEnd
        InetClose($Download)
        ConsoleWrite("END" & @LF)
    EndIf
EndFunc

 

Share this post


Link to post
Share on other sites
AutoBert

Use a Adlib registered func for check donwload:

#include <GUIConstantsEx.au3>
#include <Inet.au3>

Global $Gui = GUICreate("", 250, 100)

$Specs = GUICtrlCreateButton("Download", 20, 70, 100, 26)
$Open_Folder = GUICtrlCreateButton("Open folder", 140, 70, 100, 26)

Global $Download, $Download_Folder = @DesktopDir & "\"

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Specs
            Call(Test_1())
            ContinueLoop
        Case $Open_Folder
            ShellExecute(@DesktopDir & "\")
    EndSwitch
WEnd

GUIDelete($Gui)


Func Test_1()
    Local $File_Size = InetGetSize("http://updates3.safer-networking.org/spybot1/spybotsd162.exe", $INET_FORCERELOAD)
    ConsoleWrite($File_Size & @LF)
    $Download = InetGet("http://updates3.safer-networking.org/spybot1/spybotsd162.exe", $Download_Folder & "Spybot162.exe", $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
    $Info = InetGetInfo($Download)
    If @error Then
        Return 0
    Else
        AdlibRegister('_Downloaded', 100)
    EndIf
EndFunc   ;==>Test_1

Func _Downloaded()
    If not InetGetInfo($Download, $INET_DOWNLOADCOMPLETE) Then Return
    InetClose($Download)
    ConsoleWrite("END" & @LF)
    AdlibUnRegister('_Downloaded')
EndFunc   ;==>_Downloaded

 

Share this post


Link to post
Share on other sites
ATR

My new problem is I have multiple func like Test_1() (with download) and if I put AdlibRegister in each function, it doesn't work properly :(

 

Share this post


Link to post
Share on other sites
AutoBert

There is only one func needed watching if there is at least one handle from a backload download open. Use a Arrays for your downloadlinks and the opened/running Downloads.

Share this post


Link to post
Share on other sites
ATR

with this method I can't download multiple at once

Share this post


Link to post
Share on other sites
AutoBert
17 hours ago, ATR said:

with this method I can't download multiple at once

with this method it's possible,it's only a problem of your logic.

Edited by AutoBert

Share this post


Link to post
Share on other sites
ATR

Thank you for your help  AutoBert !

I make several tests !

But I can't use links in my array, because all my downloads are not direct links ! For each program I want download I use a function

An example of download :

Func Malwarebytes_Anti_Malware()
    $http = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $http.Open("GET", "http://data-cdn.mbamupdates.com/v0/program/mbam.check.program")
    $http.Send()
    $http.WaitForResponse()
    $source = $http.ResponseText
    $statut = $http.Status
    If $statut == 200 Then
        Local $File_Size = InetGetSize("http://data-cdn.mbamupdates.com/v0/program/data/mbam-setup-" & $source & ".exe", $INET_FORCERELOAD)
        $Telechargement = InetGet("http://data-cdn.mbamupdates.com/v0/program/data/mbam-setup-" & $source & ".exe", $Dossier_Telechargement & "Malwarebytes.exe", $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
        $Info = InetGetInfo($Telechargement)
        If Not @error Then
            While Not InetGetInfo($Telechargement, $INET_DOWNLOADCOMPLETE)
                $Avancement = InetGetInfo($Telechargement, $INET_DOWNLOADREAD)
                $Pourcentage = $Avancement * 100 / $File_Size
                GUICtrlSetData($ID_Label, Round($Pourcentage) & " %")
                Sleep(100)
            WEnd
            GUICtrlSetData($ID_Label, "100 %")
            InetClose($Telechargement)
        EndIf
    EndIf
    $http = 0
EndFunc

 

Share this post


Link to post
Share on other sites
ATR

I have multiple functions like this, and I want download in my loop

Edited by ATR

Share this post


Link to post
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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.