Jump to content

strange problem with updating control


major
 Share

Recommended Posts

Here's a small script I wrote that takes a link, and downloads whatever it leads to , to a file.

#include<GuiConstants.au3>
#include <ProgressConstants.au3>
$main = GUICreate("downloader", 300, 200)
GUICtrlCreateLabel("Enter the download link", 10, 10)
$link = GUICtrlCreateInput("", 10, 30, 200)
$fileview = GUICtrlCreateButton("...", 215, 30, 20)
$proglabel = GUICtrlCreateLabel("Progress: ", 10, 60) ;this is the first control i'm having problem with
GUICtrlSetState($proglabel, $GUI_HIDE)
$prog = GUICtrlCreateProgress(10, 90, 200, 15, $PBS_SMOOTH)
GUICtrlSetState($prog, $GUI_HIDE)
$info = GUICtrlCreateLabel("", 10, 120) ;this is the second one
;GUICtrlSetState($info, $GUI_HIDE)
$start = GUICtrlCreateButton("Start", 240, 30, 50)
GUISetState()
While 1
   $msg = GUIGetMsg()
   Select
   Case $msg = $fileview
   $filename = FileSaveDialog("Save As:", @DesktopDir, "All (*.*)", 16, "", $main)
   Case $msg = $start
   $dlink = GUICtrlRead($link)
   If $dlink <> "" Then
   GUICtrlSetState($proglabel, $GUI_SHOW)
   GUICtrlSetState($prog, $GUI_SHOW)
   Global $get = InetGet($dlink, $filename, 0, 1)
   getinfo()
   Else
   MsgBox(64, "Error", "Please enter a link.")
   EndIf
   Case $msg = $GUI_EVENT_CLOSE
   Exit
   EndSelect
WEnd
Func getinfo()
   Do
   $stats = InetGetInfo($get)
   $cmp = $stats[2]
   $total = Int($stats[1]/1024)
   $down = Int($stats[0]/1024)
   $perc = ($down / $total) * 100
   $show = "Total size : " & $total & " KB   "
   $show &= "Downloaded : " & $down & " KB"
   GUICtrlSetData($proglabel, "Progress: " & $perc & "%")  ;here on, I update the problematic controls
   GUICtrlSetData($info, $show)
   GUICtrlSetData($prog, $perc)
   sleep(1000)
   Until InetGetInfo($get, 2)
   InetClose($get)
   GUICtrlSetData($prog, 0)
   GUICtrlSetData($proglabel, "Progress: Complete")
EndFunc

The problem is that, when the download starts, updating $proglabel puts the value of $perc one line below "Progress: " also $info has nothing - like

Posted Image

And I really cant figure out whats going wrong. I've tried out different layouts, even keeping only $proglabel - no bar, no info. Same result. What am I missing?

Link to comment
Share on other sites

Try this:

#include<GuiConstants.au3>
#include <ProgressConstants.au3>

$main = GUICreate("downloader", 300, 200)
GUICtrlCreateLabel("Enter the download link", 10, 10)
$link = GUICtrlCreateInput("a", 10, 30, 200)
$fileview = GUICtrlCreateButton("...", 215, 30, 20)
$proglabel = GUICtrlCreateLabel("Progress: ", 10, 60, 200) ;this is the first control i'm having problem with
GUICtrlSetState($proglabel, $GUI_HIDE)
$prog = GUICtrlCreateProgress(10, 90, 200, 15, $PBS_SMOOTH)
GUICtrlSetState($prog, $GUI_HIDE)
$info = GUICtrlCreateLabel("", 10, 120, 280, 70) ;this is the second one
;GUICtrlSetState($info, $GUI_HIDE)
$start = GUICtrlCreateButton("Start", 240, 30, 50)
GUISetState()
$filename = @ScriptDir & "test.exe"
If FileExists($filename) Then FileDelete($filename)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $fileview
            $filename = FileSaveDialog("Save As:", @DesktopDir, "All (*.*)", 16, "", $main)
        Case $msg = $start
            $dlink = GUICtrlRead($link)
            If $dlink <> "" Then
                GUICtrlSetState($proglabel, $GUI_SHOW)
                GUICtrlSetState($prog, $GUI_SHOW)
                Global $get = InetGet($dlink, $filename, 0, 1)
                getinfo()
            Else
                MsgBox(64, "Error", "Please enter a link.")
            EndIf
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

Func getinfo()
    Do
        $stats = InetGetInfo($get)
        $cmp = $stats[2]
        $total = Int($stats[1] / 1024)
        $down = Int($stats[0] / 1024)
        $perc = Int(($down / $total) * 100 > 0)
        $show = "Total size : " & $total & " KB   "
        $show &= "Downloaded : " & $down & " KB"
        GUICtrlSetData($proglabel, "Progress: " & $perc & "%") ;here on, I update the problematic controls
        GUICtrlSetData($info, $show)
        GUICtrlSetData($prog, $perc)
        Sleep(1000)
    Until InetGetInfo($get, 2)
    InetClose($get)
    GUICtrlSetData($prog, 0)
    GUICtrlSetData($proglabel, "Progress: Complete")
EndFunc   ;==>getinfo

You have forgotten to put any value for width and height of the label controls.

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

Thank you. That worked. Width and hight are labeled as optional in the help file, so I did not think about it. Should have..

one problem still remains with calculating the percentage. Sometimes with the '> 0' it doesn't show any progress at all; when I remove the '> 0' part it works. But then I get -ve values at times. Total or download cant be -ve values..

another question.. am not sure if autoit natively supports multithreading.. but is there a way to check for gui events (like gui_close) while the loop is running?

Edit: I added little checks for $total and $down

If $total < 0 Then
     $total = $total * -1
EndIf
If $down < 0 Then
    $down = $down * -1
EndIf

and for the gui event checking part I wrote another function which is called from the getinfo() function once each iteration. Probably this looks stupid and it isnt working. I though the responces would be buffered and do the thing when I call check() but...nooo. Only after the loop ends, it responces.

Func check()
$resp = GUIGetMsg()
If $resp = $GUI_EVENT_CLOSE Then
    If $cmp = "False" Then ; I made $cmp global
    Local $msg = MsgBox (33, "Are you Sure?", "Dowload isn't complete yet. Continue?", 0, $main)
    If $msg = 1 Then
    Exit
    EndIf
    EndIf
EndIf
EndFunc
Edited by major
Link to comment
Share on other sites

Change these lines to get the progress bar to work right and the percentage to always be greater than or equal to 0.

$perc = Int(($down / $total) * 100)
        If $perc < 0 Then $perc = 0

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This line should work too:

$perc = Int(($down / $total) * 100 > 0)

Because when ($down / $total) * 100 is equal or less 0 it will become Int(False) which is always 0.

Have a look to Tiny URL Downloader. While downloading the animations is playing (pseudo multithreading).

Br,

UEZ

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

The problem though is, when it's above 0 it becomes Int(True) which is always 1.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH: ;) ops, yes you are right.

But this should work:

$perc = Int((($down / $total) * 100 > 0) * ($down / $total) * 100)

Edit: it is partial working only because Int(0/0) or Int(-1.#IND) returns always -9223372036854775807!

Br,

UEZ

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

Thanks guys for all the responses.

I have tried adlibregister but there seems to a problem with my approach. I have read the help file but cant find out yet.

The error comes up as:

$show = "Total size : " & $total & " KB   "
$show = "Total size : " & ^ ERROR

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $fileview
            $filename = FileSaveDialog("Save As:", @DesktopDir, "All (*.*)", 16, "", $main)
        Case $msg = $start
            $dlink = GUICtrlRead($link)
   If $filename <> "" Then
      If $dlink <> "" Then
                GUICtrlSetState($proglabel, $GUI_SHOW)
                GUICtrlSetState($prog, $GUI_SHOW)
                Global $get = InetGet($dlink, $filename, 0, 1)
                AdlibRegister("getinfo", 1000) ;here I register the function
            Else
                MsgBox(64, "Error", "Please enter a link.")
    EndIf
    Else
    MsgBox(64, "Error", "Please enter a save location.")
   EndIf
        Case $msg = $GUI_EVENT_CLOSE
            Exit
   EndSelect
WEnd


Func getinfo()
    If InetGetInfo($get, 2) = "False" Then
        $stats = InetGetInfo($get)
        Global $cmp = $stats[2]
        $total = Int($stats[1] / 1024)
        $down = Int($stats[0] / 1024)
  If $total < 0 Then
     $total = $total * -1
  EndIf
  If $down < 0 Then
      $down = $down * -1
  EndIf
  $perc = Int(($down / $total) * 100)
        $show = "Total size : " & $total & " KB   "
        $show &= "Downloaded : " & $down & " KB" & @CRLF & @CRLF
  $show &= "Percentage : " & $perc & " %   "
     $show &= "Completed : " & $cmp
        GUICtrlSetData($proglabel, "Progress: " & $perc & "%") ;here on, I update the problematic controls
        GUICtrlSetData($info, $show)
        GUICtrlSetData($prog, $perc)
  EndIf
    AdlibUnRegister() ;unregister it when dlod is over
    InetClose($get)
$show = "Total size : " & $total & " KB   "
    $show &= "Downloaded : " & $total & " KB" & @CRLF & @CRLF
    $show &= "Percentage : " & "100" & " %   "
    $show &= "Completed : " & "True"
GUICtrlSetData($info, $show)
    GUICtrlSetData($proglabel, "Progress: Complete")
GUICtrlSetData($prog, 100)
EndFunc
Edited by major
Link to comment
Share on other sites

You should be declaring the variable $total at the start of the function, because the way it's written now, the only time the function sees $total is if If InetGetInfo($get, 2) = "False" , and you run this function once per second. Put a Local $total = 0 (or whatever you want it to equal) right after the Func line.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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