Jump to content

Switch - case - continuecase


 Share

Recommended Posts

Hi all,

I have a problem with my conditions switch...case.

This code is an example of my problem :

$hGUI = GUICreate("Example", 250, 80)

$b1 = GUICtrlCreateButton("Wait function", 20, 20, 100)
$b2 = GUICtrlCreateButton("MsgBox", 20, 50, 100)

GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $b1
            Wait()
        Case $b2
            MsgBox(64,"", "$b2 button")
    EndSwitch
WEnd

GUIDelete($hGUI)

Func Wait()
    Sleep(15000)
EndFunc

I have my switch case statement with to conditions -> $b1 and $b2

If I click on the "Wait Function" button, I sleep 15 seconds and if I click the "MsgBox" button, I must wait until 15 seconds are not finished !

I'm testing to put ContinueCase after calling Wait() but it doesn't work :(

 

Link to comment
Share on other sites

  • Moderators

ATR,

The Interrupting a running function tutorial in the Wiki explains why this happens. To solve the problem you can either use one of the techniques described in that tutorial or just look for events while waiting - like this:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

$hGUI = GUICreate("Example", 250, 80)

$b1 = GUICtrlCreateButton("Wait function", 20, 20, 100)
$b2 = GUICtrlCreateButton("MsgBox", 20, 50, 100)

GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $b1
            Wait()
        Case $b2
            MsgBox(64,"", "$b2 button")
    EndSwitch
WEnd

GUIDelete($hGUI)

Func Wait()
        ConsoleWrite("Wait started" & @CRLF)
    $nBegin = TimerInit()
    Do
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $b2
                MsgBox(64,"", "$b2 button")
        EndSwitch
    Until TimerDiff($nBegin) > 15000
    ConsoleWrite("Wait ended" & @CRLF)
EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

2 hours ago, ATR said:

I don't want stop the function but only use other case during the function. 

Have you tried Melba23's script? Seems like it does exactly what you ask.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

I think what I want to do is not possible :-(

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

$hGUI = GUICreate("Example", 250, 80)

$b1 = GUICtrlCreateButton("Download", 20, 20, 100)
$b2 = GUICtrlCreateButton("MsgBox", 20, 50, 100)

GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $b1
            Download()
        Case $b2
            MsgBox(64,"", "$b2 button")
    EndSwitch
WEnd

GUIDelete($hGUI)

Func Download()
    $File_Size = InetGetSize("http://download.bleepingcomputer.com/Xplode/AdwCleaner.exe", $INET_FORCERELOAD)
    $Telechargement = InetGet("http://download.bleepingcomputer.com/Xplode/AdwCleaner.exe", $Dossier_Telechargement & "AdwCleaner.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)
            ContinueLoop
        WEnd
        InetClose($Telechargement)
    EndIf
EndFunc

But thanks anyway

Link to comment
Share on other sites

Use INET_DOWNLOADBACKGROUND without waiting for the download using ( while / do loops) because it is downloading asynchronously , but wait for the download result using (while / do loops) is not correct at least in your case. basically you are forcing the download to be synchronous.

 

Saludos

Link to comment
Share on other sites

1 hour ago, ATR said:

I think what I want to do is not possible :-(

What do you want to do? I think you will make some other things while DL in running in background. This is possible, here a little script just for proof of concept:

#include <WinAPI.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <Array.au3>
#include <Date.au3>

Opt('MustDeclareVars', 1)

Global $iWatch, $iRes, $bWorking

Global $sLocalPath = @ScriptDir & "\TestDLs\"
If Not FileExists($sLocalPath) Then DirCreate($sLocalPath)

Global $aFiles[5][6] ;zeile 0 für Gesamtgrösse Spalten 2=Grösse aller DownLoaddateien, 3=bereits downheloaded, 4 Bytes durch Neustart verworfen,
;danach Spalten 0=URL, 1=Lokaler Pfad, 2=DL-Grösse,3=DL-Bytes , 4=DL-Handle, 5=Fehler Anzahl
Global $aData[1]
Global $aKBs[10]
$aFiles[0][0] = 4 ;Anzahl der Downloads

$aFiles[1][0] = "http://download.bleepingcomputer.com/Xplode/AdwCleaner.exe"
$aFiles[1][1] = "AdwCleaner.exe"
$aFiles[2][0] = "http://download.iobit.com/action-center/asc920-0330.exe"
$aFiles[2][1] = "Advanced_SystemCare.exe"
$aFiles[3][0] = "http://download.geo.drweb.com/pub/drweb/cureit/drweb-cureit.exe"
$aFiles[3][1] = "DrWeb-CureIt.exe"
$aFiles[4][0] = "http://data-cdn.mbamupdates.com/web/JRT.exe"
$aFiles[4][1] = "JRT.exe"

Dim $hGUI_c[$aFiles[0][0] + 2]
Dim $idprgInfo[$aFiles[0][0] + 2]
Dim $idlblInfo[$aFiles[0][0] + 2]

Global $msg, $sLblText, $tdStart

Global $hGuiMain = GUICreate("Multi-Downloader 0.5.2.4", 325, 250)
Global $button = GUICtrlCreateButton("Start", 5, 20, 70, 20)
Global $Display = GUICtrlCreateButton("Debug", 5, 70, 70, 20)
Global $Arrival = GUICtrlCreateLabel("", 95, 25, 185, 50)
GUICtrlSetFont(-1, 15, 800)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

For $i = 1 To $aFiles[0][0]
    _MakeDLPanel($i, $aFiles[$i][1])
    GUISwitch($hGuiMain)
Next
_MakeDLPanel($i, 'Gesamt')
Global $idlblKBs = GUICtrlCreateLabel('', 95, 4, 150, 60)
GUICtrlSetFont(-1, 15, 800)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUISwitch($hGuiMain)
GUISetState()

Func _MakeDLPanel($iNo, $sText)
    $idprgInfo[$iNo] = GUICtrlCreateProgress(8, 70 + $iNo * 30, 310, 30)
    $hGUI_c[$iNo] = GUICreate("", 309, 30, 8, 70 + $iNo * 30, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TRANSPARENT, $WS_EX_MDICHILD), $hGuiMain)
    GUISetBkColor(0x989898, $hGUI_c[$iNo])
    GUICtrlCreateLabel($sText, 0, 8, 220, 25, $ES_LEFT)
    GUICtrlSetFont(-1, 12, 1000)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    $idlblInfo[$iNo] = GUICtrlCreateLabel("0,0 % ", 221, 8, 90, 25, $ES_RIGHT)
    GUICtrlSetState(-1, $GUI_HIDE)
    GUICtrlSetFont(-1, 12, 1000)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    _WinAPI_SetLayeredWindowAttributes($hGUI_c[$iNo], 0x989898)
    GUISetState(@SW_SHOWNA, $hGUI_c[$iNo])
EndFunc   ;==>_MakeDLPanel

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            $iRes=$IDNO
            If $bWorking Then $iRes=MsgBox($MB_YESNO+$MB_ICONQUESTION,'Sure',"there are some DL's to be continued"&@CRLF&"Realy exit?")
            If $iRes=$IDYES Then Exit
        Case $Display
            _ArrayDisplay($aFiles)
        Case $button
            $tdStart = TimerInit()
            For $i = 1 To $aFiles[0][0]
                $aFiles[$i][4] = InetGet($aFiles[$i][0], $sLocalPath & $aFiles[$i][1], 1, 1)
                GUICtrlSetState($idlblInfo[$i], $GUI_SHOW)
            Next
            GUICtrlSetState($idlblInfo[$i], $GUI_SHOW)
            AdlibRegister('_WatchDL', 10)
            AdlibRegister('_checkArrival',1000)
            ;_ArrayDisplay($aFiles)
    EndSwitch
WEnd

Func _WatchDL()
    ;Local Static $iOld
    Local $iDiff
    Local $nPercent
    $iWatch += 1
    If $iWatch > $aFiles[0][0] Then $iWatch = 1
    If $aFiles[$iWatch][4] > 0 Then
        $aData = InetGetInfo($aFiles[$iWatch][4], -1)
        If @error Then
            ConsoleWrite(@error & @TAB & @extended & @CRLF)
            Return
        EndIf
        If $aFiles[$iWatch][2] = 0 Then
            $aFiles[$iWatch][2] = $aData[1] ;Downloadgrösse ermittelt
            $aFiles[0][2] += $aFiles[$iWatch][2] ;und auch zur Gesamtdownloadgrösse hinzuzählen
        EndIf
        $nPercent = Round($aData[0] / $aFiles[$iWatch][2] * 100, 1)
        $nPercent = StringReplace($nPercent, '-1.#IND', '')
        $aFiles[$iWatch][3] = $aData[0]
        If Int($nPercent) <> GUICtrlRead($idprgInfo[$iWatch]) Then _
                GUICtrlSetData($idprgInfo[$iWatch], Int($nPercent))
        If $nPercent <> Number(GUICtrlRead($idlblInfo[$iWatch])) Then _
                GUICtrlSetData($idlblInfo[$iWatch], _FormatString($nPercent))
        If $aData[2] Then
            If $aData[3] Then
                InetClose($aFiles[$iWatch][4])
                $aFiles[$iWatch][4] = 'Erfolg'
                GUICtrlSetData($idprgInfo[$iWatch], 100)
            Else
                $aFiles[$iWatch][5] += 1
                If $aFiles[$iWatch][5] < 10 Then
                    $aFiles[$iWatch][4] = InetGet($aFiles[$iWatch][0], $sLocalPath & $aFiles[$iWatch][1], 1, 1)
                    $aFiles[0][4] += $aFiles[$iWatch][3]
                Else
                    $aFiles[0][2] -= $aFiles[$iWatch][2]; von gesamt DL-größe abziehen
                    $aFiles[$iWatch][2] = 0
                    $aFiles[$iWatch][3] = 0
                    $aFiles[$iWatch][4] = 'Abbruch'
                    GUICtrlSetData($idprgInfo[$iWatch], 0)
                    GUICtrlSetData($idlblInfo[$iWatch], 'Abbruch')
                EndIf
            EndIf
        EndIf
        $aFiles[0][3] = 0
        For $iWatch = 1 To $aFiles[0][0]
            $aFiles[0][3] += $aFiles[$iWatch][3]
        Next
        #cs
            If $aFiles[0][3] <> 0 Then $iDiff = $iOld - $aFiles[0][3]
            _ArrayPush($aKBs, $iDiff)
            Local $nKBs = 0
            For $i = 0 To UBound($aKBs) - 1
            $nKBs += $aKBs[$i]
            Next
            $nKBs = Round($nKBs / 10240, 2)
            GUICtrlSetData($idlblKBs, _FormatString($nKBs, 'KByte/sec'))
            $iOld=$aFiles[0][3]
        #ce
        $nPercent = Round($aFiles[0][3] / $aFiles[0][2] * 100, 1)
        $nPercent = StringReplace($nPercent, '-1.#IND', '')
        If Int($nPercent) <> GUICtrlRead($idprgInfo[$iWatch]) Then _
                GUICtrlSetData($idprgInfo[$iWatch], Int($nPercent))
        If $nPercent <> Number(GUICtrlRead($idlblInfo[$iWatch])) Then _
                GUICtrlSetData($idlblInfo[$iWatch], _FormatString($nPercent))
    EndIf
    $bWorking = False
    For $i = 1 To $aFiles[0][0]
        $bWorking = ($aFiles[$i][4] > 0)
        If $bWorking Then ExitLoop
    Next
    If Not $bWorking Then
        _ArrayDisplay($aFiles, 'Downloads beendet')
        AdlibUnRegister('_WatchDL')
        AdlibUnRegister('_checkArrival')
    EndIf
EndFunc   ;==>_WatchDL

Func _checkArrival()
    Local $iOld
        Local $iElapsed = TimerDiff($tdStart) / 1000
        Local $nKBs = Round(($aFiles[0][3] + $aFiles[0][4]) / $iElapsed / 1024, 1)
        If $nKBs <> Number(GUICtrlRead($idlblKBs)) Then _
                GUICtrlSetData($idlblKBs, _FormatString($nKBs, 'KB/s'))
        Local $iRemaining = Int(($aFiles[0][2] - $aFiles[0][3]) / $nKBs / 1024)
        if $iRemaining<>$iOld Then _
            GUICtrlSetData($Arrival, _FormartRestTime($iRemaining))
        $iOld=$iRemaining
        ConsoleWrite('noch: ' & $iRemaining & ' Sekunden!' & @CRLF)
EndFunc

Func _FormartRestTime($iSec)
    Local $sRet
    If $iSec<0 Then
        $sRet='unbekante Dauer'
        Return $sRet
    EndIf
    Local $iHour=Int($iSec/3600), $iMin
    if $iHour Then
        AdlibRegister('_checkArrival',60000)
        $iMin=Mod($iSec,3600)
        $sRet='noch ca. '&$iHour&','&$iMin &' Stunden'
    Else
        $iMin=Int($iSec/60)
        If $iMin Then
            AdlibRegister('_checkArrival',10000)
            $sRet='noch ca. '& $iMin &' Minuten'
        Else
            AdlibRegister('_checkArrival')
            $sRet='noch ca. '& $iSec &' Sekunden'
        EndIf
    EndIf
    Return $sRet
EndFunc

Func _FormatString($number, $sExt = '% ')
    If Not StringInStr($number, '.') Then $number &= '.0'
    If $number = '.0' Then $number = '0.0'
    Return StringReplace(StringFormat("%8s " & $sExt, $number), '.', ',')
EndFunc   ;==>_FormatString

as you can see there are multiple DL's possible and nearly realtime the progress for each is updated. And you can also get the Debug Arraydisplay while DL's are running.

Edited by AutoBert
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...