PB program window freezes

Just starting out? Need help? Post your questions and find answers here.
PitH
User
User
Posts: 16
Joined: Sun Jan 16, 2022 6:12 am

PB program window freezes

Post by PitH »

Hello
I'm not new to PB but normally use comand line executeables
I was trying several times on different computers and different windows version to create window frontend.

I allways ran into the same problem and skipped window frontends but sometimes it would be much better to make use of.

My problem is that the program windows allways freeze if i start another programm or another windows placed on top.
it doesn't depent on computing power or graphic card ... so what i'm doing wrong ?
place here an example code and hopefully receive a reply of someone. Thanks

used PB version 5.73 but same happens with other version.

Code: Select all

#WIN_MAIN = 0

#Button_Program_End = 9

#Text_Line1 = 20

Global Quit.b = #False

#FLAGS = #PB_Window_ScreenCentered
OpenWindow(#WIN_MAIN, 0, 0, 200, 100, "Pits-FTP-Tool V1.0  © @P. Hermann", #FLAGS)
If UseGadgetList(WindowID(#WIN_MAIN))

ButtonGadget(#Button_Program_End, 50, 100, 60, 25, "Exit") 
TextGadget(#Text_Line1, 5, 50, 195, 25, "", #PB_Text_Center)
EndIf

SetActiveGadget(#Button_Program_End)

If LoadFont(99, "Arial", 10, #PB_Font_Bold)
    SetGadgetFont(#Text_Line1, FontID(99))
EndIf  

Repeat

Event.l = WaitWindowEvent()
Select Event.l
  Case #PB_Event_Gadget
    Select EventGadget()
      Case #Button_Program_End
        Quit.b = #True
     
  EndSelect
EndSelect

Gosub Wait

Until Quit.b = #True

End

Wait:

While (FormatDate("%hh:%ii", Date())) <> FTP_Start$
  SetGadgetText(#Text_Line1,"Time : " + FormatDate("%hh:%ii:%ss", Date()))
  Delay(1000)
Wend

Return
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: PB program window freezes

Post by BarryG »

Here's my way of doing your code. Not sure if it helps because we don't know the full context of your app, but try this for a start.

Code: Select all

#WIN_MAIN = 0

#Button_Program_End = 9

#Text_Line1 = 20

Global Quit.b = #False

Global FTP_Start$ ; Global so it can be used inside the UpdateClock() procedure.

#FLAGS = #PB_Window_ScreenCentered
OpenWindow(#WIN_MAIN, 0, 0, 200, 100, "Pits-FTP-Tool V1.0 © @P. Hermann", #FLAGS)
If UseGadgetList(WindowID(#WIN_MAIN))
  ButtonGadget(#Button_Program_End, 50, 100, 60, 25, "Exit")
  TextGadget(#Text_Line1, 5, 50, 195, 25, "", #PB_Text_Center)
EndIf

SetActiveGadget(#Button_Program_End)

If LoadFont(99, "Arial", 10, #PB_Font_Bold)
  SetGadgetFont(#Text_Line1, FontID(99))
EndIf

Procedure UpdateClock()
  dt.q = Date()
  If FormatDate("%hh:%ii", dt) <> FTP_Start$
    SetGadgetText(#Text_Line1,"Time : " + FormatDate("%hh:%ii:%ss", dt))
  EndIf
EndProcedure

UpdateClock() ; Show clock immediately instead of waiting for the first timer trigger.

AddWindowTimer(#WIN_MAIN, 0, 500) ; Updates the clock every half-second.

Repeat
  
  Event.l = WaitWindowEvent()
  Select Event.l
    Case #PB_Event_Timer
      UpdateClock()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_Program_End
          Quit.b = #True
      EndSelect
  EndSelect
  
Until Quit.b = #True

End
PitH
User
User
Posts: 16
Joined: Sun Jan 16, 2022 6:12 am

Re: PB program window freezes

Post by PitH »

Thanks a lot for your suggestion, i will give it a try later

the rest of code isn't important for the error
this shrted code produce the same effect like the longer on.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PB program window freezes

Post by collectordave »

Hi

Are you trying to time some action?

One point in your original code

Code: Select all

Wait:

While (FormatDate("%hh:%ii", Date())) <> FTP_Start$
SetGadgetText(#Text_Line1,"Time : " + FormatDate("%hh:%ii:%ss", Date()))
Delay(1000)
Wend

Return
will simply run forever as FTP_Start$ has not been initialised.

The delay(1000) is also a blocking delay so basically when in the loop your programme will do nothing else.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
PitH
User
User
Posts: 16
Joined: Sun Jan 16, 2022 6:12 am

Re: PB program window freezes

Post by PitH »

Hi

It should be just a waiting loop to wait from now to the advised time

FTP_Start$ is initialised , the loop works as long as i do not try to move the window or start another programm like for example notepad
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PB program window freezes

Post by collectordave »

Just some quick code:

Code: Select all



Global Win_Main.i
Global ButtonExit.i,btnStart.i
Global Wait_Until.l

Global Quit.b = #False


Procedure ChooseTime()
  
  Wait_Until = Date() + 2 ; adding seconds can use adddate()
  
EndProcedure

Procedure Check_Time()
  
  If Wait_Until > 0
  
  If Date() > Wait_Until
    
    Debug "The Wait is over"
    Wait_Until = 0
    
  EndIf
EndIf

  EndProcedure
  
  


Win_Main = OpenWindow(#PB_Any, 0, 0, 400, 200, "Pits-FTP-Tool V1.0 © @P. Hermann", #PB_Window_ScreenCentered)

ButtonExit = ButtonGadget(#PB_Any, 5, 30, 60, 25, "Exit") 
btnStart = ButtonGadget(#PB_Any, 5, 60, 60, 25, "Start") 

AddWindowTimer(Win_Main,123,100)



Repeat
     Event = WaitWindowEvent()
     
     Select Event
       Case #PB_Event_Timer
         Check_Time()
       Case #PB_Event_Gadget
         Select EventGadget()
           Case ButtonExit 
             Quit = #True
           Case btnStart 
             ChooseTime()
         EndSelect
     
     EndSelect
 
Until Quit.b = #True

End
Clicking the start button will add 2 seconds to the current time and then wait for it to finish 2 seconds later.

The event loop is still monitored so should not freeze.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
PitH
User
User
Posts: 16
Joined: Sun Jan 16, 2022 6:12 am

Re: PB program window freezes

Post by PitH »

Hey guys
you made my day !

both versions solve the problem
I took the version of Barry cause it was easier to implement in my code

i must take a closer look to understand the problem and the difference in code.

THANK YOU BOTH VERY MUCH
8)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PB program window freezes

Post by collectordave »

Thats good.

The main thing to remember is that your programme must always process window events when it stops the program freezes.

if you add two debug lines to your original code as below:

Code: Select all

Wait:

While (FormatDate("%hh:%ii", Date())) <> FTP_Start$
  
  Debug "CheckFTP$ " + FTP_Start$  
  SetGadgetText(#Text_Line1,"Time : " + FormatDate("%hh:%ii:%ss", Date()))
  Delay(1000)
Wend

debug "Leaving wait sub"

Return
you will see that every second "CheckFTP$" is displayed in the debug window but never the leaving statement.

Note FTP_Start$ is an empty string

Hope that helps a little
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: PB program window freezes

Post by Marc56us »

Also, study the Parsedate() and AddDate() functions, they will allow you to do tests and calculations on dates directly rather than comparing strings at each iteration.
ElapsedMilliseconds() is also usefull for timeout.

:wink:
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB program window freezes

Post by mk-soft »

@collectordave and All

Sorry it sounds a bit harsh.

NEVER USE DELAY IN GUI FUNCTIONS. ALWAYS PROCESS ALL EVENTS IN AN EVENT LOOP.

Every change to Gadget also causes messages in the operating system. These are only processed in the event loop (WaitWindowEvent).
For time events there is AddWindowTimer. It is best to bind these to a procedure with BindEvent.

Store time-critical, long and asynchronous processes in threads and send information from the thread to the GUI with PostEvent.

If this rule is not observed, the message buffer of the operating system will overflow at some point and the message 'Program not running' will appear.

Code: Select all

;-TOP

Enumeration Windows
  #Main
EndEnumeration

Enumeration Gadgets
  
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

Procedure DoEventTimer100()
  Protected info.s
  info = " Time: " + FormatDate("%HH:%II:%SS", Date())
  StatusBarText(#MainStatusBar, 0, info)
EndProcedure

Procedure DoEventSizeWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
  ; Rezize Gadgets
EndProcedure

Procedure Main()
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Window" , #MainStyle)
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    AddWindowTimer(#Main, 100, 1000)
    
    BindEvent(#PB_Event_SizeWindow, @DoEventSizeWindow(), #Main)
    BindEvent(#PB_Event_Timer, @DoEventTimer100(), #Main, 100)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
Examples of how to outsource asynchronous processes to threads are available here. Also how to send strings to the GUI without losing them are included in the example.

Link: Mini Thread Control
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
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PB program window freezes

Post by collectordave »

Not harsh at all just the same point I was attempting to make.

One that is easy to miss is where the event loop is stalled as in the case above. The wait: go sub could not end so no more events processed from the first time the event loop is triggered.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply