StatusBarProgress doesn't update the window unless an event occurs

Just starting out? Need help? Post your questions and find answers here.
elwood
User
User
Posts: 30
Joined: Tue Dec 27, 2016 4:40 pm
Location: Lyon, France

StatusBarProgress doesn't update the window unless an event occurs

Post by elwood »

Hi brave coders,

I have a problem with the following code.
When I run it, the window with the progress bar opens but the bar is not refreshed (but the download runs correctly).
As soon as I click on the progress window, or just moving my mouse over it and the refresh starts.

What can be wrong? (I use PB 6.04)
You can run the code and download from my server. No problem. This file is publicly available anyway.

Thanks.

Code: Select all

Global ftp

Procedure myGetFile(file$)
  download = 0
  ; we retrieve information on file in FTP
  If ExamineFTPDirectory(ftp)
    While NextFTPDirectoryEntry(ftp)
      If FTPDirectoryEntryName(ftp) = file$
        dateftp = Val(FormatDate("%yyyy%mm%dd",FTPDirectoryEntryDate(ftp)))
        sizeftp = FTPDirectoryEntrySize(ftp)
        Break
      EndIf
    Wend
    FinishFTPDirectory(ftp)
  EndIf
  ; we compare size and date between source and destination files
  dateloc = 19000101
  sizeloc = FileSize(file$)
  ; if local file not found
  If sizeloc < 0
    download = 1
  Else
    ; if local file exists locally, we have to compare with file in FTP
    If sizeloc >= 0
      dateloc = Val(FormatDate("%yyyy%mm%dd", GetFileDate(file$, #PB_Date_Modified)))
    EndIf
  EndIf
  
  ; if file in FTP is more recent than local one
  If dateftp > dateloc
    download = 1
  EndIf
  ; if file in FTP has same date but different size
  If (dateftp = dateloc And sizeftp <> sizeloc)
    download = 1
  EndIf

  If download = 1
    If OpenWindow(0, #PB_Ignore, #PB_Ignore, 340, 50, "StatusBarProgress", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
      statusbar = CreateStatusBar(0, WindowID(0))
      If statusbar
        AddStatusBarField(150)
        AddStatusBarField(#PB_Ignore)
        StatusBarText(0, 0, "Download in progress...")
        StatusBarProgress(0, 1, 1)
      EndIf
        
      res = ReceiveFTPFile(ftp, file$, file$, #True)
      If Not res
        MessageRequester("Erreur", "cannot download " + file$)
        CloseWindow(#PB_All)
        ProcedureReturn
      EndIf

      Repeat
        res = FTPProgress(ftp)  
        If WaitWindowEvent() = #PB_Event_CloseWindow
          AbortFTPFile(ftp)
          CloseWindow(#PB_All)
          Break
        EndIf
        If res = #PB_FTP_Error
          MessageRequester("Erreur", "Error downloading " + file$)
          Break
        EndIf
        If res = #PB_FTP_Finished
          Break
        EndIf
        If statusbar
          StatusBarProgress(0, 1, Int(res / sizeftp * 100))
        EndIf
        Delay(100)
      ForEver
      FreeStatusBar(#PB_All)
      ProcedureReturn

    EndIf ; OpenWindow
    
  EndIf ; download = 1
EndProcedure

#login = "login"
#password = "pass"

ftp = OpenFTP(#PB_Any, "ftp.domain.fr", #login, #password)
If Not ftp
  MessageRequester("Erreur", "cannot connect")
  End 1
EndIf 

myGetFile("testfile.exe")

CloseFTP(ftp)
Last edited by elwood on Thu Dec 28, 2023 9:31 am, edited 2 times in total.
AmigaOS betatester
French Amiga Translation Team
Team for the Planet member
pjay
Enthusiast
Enthusiast
Posts: 251
Joined: Thu Mar 30, 2006 11:14 am

Re: StatusBarProgress doesn't update the window unless an event occurs

Post by pjay »

Try changing your WaitWindowEvent() to just WindowEvent() (as you have a delay in your loop anyway), or add a timeout; WaitWindowEvent(100).
elwood
User
User
Posts: 30
Joined: Tue Dec 27, 2016 4:40 pm
Location: Lyon, France

Re: StatusBarProgress doesn't update the window unless an event occurs

Post by elwood »

Yes !!!
I kept looking at my code and doing some tests but without changing WaitWindowEvent.
You are right I don't have to wait as I handle some other cases where I should exit the main loop.

Thanks pjay.
AmigaOS betatester
French Amiga Translation Team
Team for the Planet member
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: StatusBarProgress doesn't update the window unless an event occurs

Post by mk-soft »

Remove Delay(100) from the event loop, otherwise the accumulated events will not be processed fast enough

WaitWindowEvent(100)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: StatusBarProgress doesn't update the window unless an event occurs

Post by Marc56us »

Code: Select all

            Repeat
                res = FTPProgress(ftp)  
                If WaitWindowEvent() = #PB_Event_CloseWindow
                    AbortFTPFile(ftp)
                    CloseWindow(#PB_All)
                    Break
                Else                                ; --- Add
                    While WindowEvent() : Wend      ; --- Add (one -single- loop)
                EndIf
[...]
Or add this single loop to another location if it's called too often.
https://www.purebasic.fr/english/viewto ... 77#p599677
Post Reply