Page 1 of 1

StatusBarProgress doesn't update the window unless an event occurs

Posted: Thu Dec 28, 2023 12:18 am
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)

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

Posted: Thu Dec 28, 2023 8:46 am
by pjay
Try changing your WaitWindowEvent() to just WindowEvent() (as you have a delay in your loop anyway), or add a timeout; WaitWindowEvent(100).

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

Posted: Thu Dec 28, 2023 9:30 am
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.

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

Posted: Fri Dec 29, 2023 10:44 am
by mk-soft
Remove Delay(100) from the event loop, otherwise the accumulated events will not be processed fast enough

WaitWindowEvent(100)

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

Posted: Fri Dec 29, 2023 11:50 am
by AZJIO

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

Posted: Fri Dec 29, 2023 12:01 pm
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